3

Below is the code I have to fetch all Discussions with the associated threads and posts. I am trying to limit the number of posts to 1. When I do this, I expect for each thread to have only 1 post in it, however 9 threads have no posts in them and 1 thread has 1 post in it.

App\Discussion::with([
    'threads' => function ($query) => {
        // Correctly limits the number of threads to 10.
        $query->latest()->take(10);
    },
    'threads.posts' => function ($query) {
        // Limits all posts to 1, not each post to 1 for each thread.  
        $query->take(1);
    }
])->where(['slug' => 'something'])->firstOrFail();

The code above runs the following SQL queries in the database.

select * from `discussions` where (`slug` = ?) and `discussions`.`deleted_at` is null limit 1

select * from `threads` where `threads`.`discussion_id` in (?) and `threads`.`deleted_at` is null order by `created_at` desc limit 10

select * from `posts` where `posts`.`thread_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `posts`.`deleted_at` is null order by `created_at` desc limit 1

I can see the third query is the one causing the issue as it's limiting all posts to 1.

I expect to see the following; one discussion, with 10 threads and 1 post in that thread.

{
  "id": 1,
  "title": "Discussion Title",
  ...
  "threads": [
    {
      "id": 1,
      "title": "Thread Title",
      ...
      "posts": [
        {
          "id": 1,
          "title": "Post Title",
          ...
        }
      ]
    }
    ...
  ]
}

Is there a way to do this within the Laravel framework or will I have to run raw queries? I would prefer to stick with the Eloquent ORM as much as possible.

1
  • Please show your thread model code, specifically the "public function posts()". Is it a many to many or one to many relation? Jun 10, 2016 at 12:00

2 Answers 2

4

You need to make a few "tweaks", as Eloquent does not have a query method to limit the number results in a relation.

First create this function on your Thread model.

public function one_post()
{
    //OrderBy is optional
    return $this->hasOne(Post::class)->orderBy('id', 'asc');
}

Now you have a relation that will only return one post. You can have your query this way:

    App\Discussion::with([
        'threads' => function ($query) {
            // Correctly limits the number of threads to 10.
            $query
                ->latest()
                ->take(10)
                ->with('one_post');
        }
        ])->where(['slug' => 'something'])->firstOrFail();
7
  • 2
    Have you tried this? I would expect this to return 10 threads total, not ten 10 threads per discussion. That inner query should only be executed once. Eloquent will do one query to get all the discussions then one query to get all the threads. So if you modify the second query to only take 10, it will only get 10 in for everything.
    – jfadich
    Jun 9, 2016 at 17:22
  • 1
    @jfadich This query will return the first Discussion where "slug" is "something", with the 10 latest Threads loaded as a relation. Each thread will have 1 post loaded as a relation. Jun 9, 2016 at 17:25
  • You're right, I mistakenly thought it getting a collection of discussions, not a single discussion. I missed it was firstOrFail() and not get()
    – jfadich
    Jun 9, 2016 at 17:27
  • This does the same as what I posted in my question. It limits all posts to 1, not each post relationship to 1.
    – Enijar
    Jun 10, 2016 at 7:58
  • @Enijar have you actually tried my code? If you have your models correctly set it will give one post per thread, and every post will be related to it's respective thread. Jun 10, 2016 at 11:55
1

Eager result cannot be easily limited, but according to: https://laracasts.com/discuss/channels/eloquent/laravel-51-eloquent-relationship-hasmany-limit-records can be mapped using the model relation....in other words your code could look something like:

 App\Discussion::with([
        'threads' => function ($query) => {
            // Correctly limits the number of threads to 10.
            $query->latest()->take(10);
        }])->where(['slug' => 'something'])->firstOrFail()
        ->map(function ($thread) {
            $thread->setRelation('posts', $thread->posts->take(1));
            return $thread;
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.