Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:45:24+00:00 2026-06-17T11:45:24+00:00

With Laravel and the eloquent ORM, I want to create an array or object

  • 0

With Laravel and the eloquent ORM, I want to create an array or object of all posts and corresponding comments that belong to a specific user (the logged in one). The result will then be used with Response::eloquent(); to return JSON.

Basically in pseudo-code:

All Posts by user ::with('comments').

or

Posts by Auth::user()->id ::with('comments').

I have my database setup per the usual with a user’s table, comments table and posts table. The comments table has a post_id and the posts table has a user_id.

The long way of doing this without Laravel would be something like:

SELECT * FROM posts WHERE user_id = 'user_id'
foreach($result as $post) {
    SELECT * FROM comments WHERE posts_id =  $post->id
    foreach($query as $comment) {
        $result[$i]->comments[$n] = $comment
    }
}

But I want to accomplish it with Laravel’s Eloquent ORM.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:45:26+00:00Added an answer on June 17, 2026 at 11:45 am

    It looks like you don’t even need a nested eager load, you just need to modify the query that with returns, so:

    $posts = Post::with('comments')->where('user_id', '=', 1)->get();
    

    You can daisy chain most of the methods in the Eloquent system, generally they’re just returning a Fluent query object.

    (I haven’t tested it but I’m fairly certain that’ll work. Also, you can’t do it on ::all() because that calls ->get() for you. You have to dig in the source code to find this, I don’t think the Eloquent documentation mentions that’s what it’s doing.)

    Also the Eager Loading Documentation covers nested eager loading, so you could load all users, with their posts, with the comments:

    You may even eager load nested relationships. For example, let’s
    assume our Author model has a “contacts” relationship. We can eager
    load both of the relationships from our Book model like so:

    $books = Book::with(array('author', 'author.contacts'))->get();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying out Laravel 4. When using the ORM to create a new user
How can we perform bulk database insertions in Laravel using Eloquent ORM? I am
I'm trying to use route filters in laravel to check whether a specific user
I want to build a menu from an array in Laravel. What I'm currently
I'm getting confused with the various functions in Laravel's Eloquent ORM and what they
I just started learning Laravel framework today. I would like to check for user's
Man, this is getting frustrating! In Laravel 4, using Eloquent, this works: var_dump(Worker::find(1)); .
I just created a simple app in Laravel 4 and when I create a
I read that Laravel 4 is not going to change much on the surface,
I started using Laravel (I added RoR as a tag since I know that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.