0

I have custom query where I get services by grouping city and select from services table only custom fields by loading there reviews:

$services = City::select('id')
                ->has('services')
                ->with([
                    'services:id,title',
                    'services.reviews'
                ])->get();

In this case I need to only 3 reviews to each service with reviews custom fields. For example I need from reviews table only description and rate. How I can do it in my case?

Andreas Hunter
  • 4,504
  • 11
  • 65
  • 125
  • 1
    https://stackoverflow.com/a/56010673/4575350 – STA Sep 08 '20 at 09:42
  • Does this answer your question? [Limiting the result of nested relationship in Laravel](https://stackoverflow.com/questions/55991576/limiting-the-result-of-nested-relationship-in-laravel) – shock_gone_wild Sep 08 '20 at 09:44

1 Answers1

0

You could just create a relationship that limits the child to three rows. Then you'll have the ability to specify what columns you want to show in the same way you did with services.

So for example, in the Service model, add something like:

public function threeReviews() {
    return $this->reviews()->take(3);
}

Then use it the same way you use the services.

->with(['services:id,title','services.reviews:description,rate'])