Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amjadkhan896's avatar

laravel closure query with limit

Hi all i have a closure query like below.

$motors = Motor::with(['advertisementPhotos', 'customer.dealer', 'adAttributes' => function ($query) { $query->with('attribute'); // $query->where('content', 'like', 'foo%'); //; $query->limit(2);

    }])->where('featured', 1)->where('status', 1)
        ->where('date_expiry', '>=', date('Y-m-d') . ' 00:00:00')
        ->limit(20)->orderBy('position', 'asc');

In above query in the adAttribute relationship i want to put limit of 2. I want to show just 2 attributes for each motor. the attributes are "milage, color etc." The issue is when i put the limit then it puts the limit for all the motors entity. and does not display any attributes for the second and third and so on motors. Because I put the limit of 2.

And i want all the motors should be loaded with the 2 attributes. Anybody here to help me in this regards.

Thanks in advance

0 likes
0 replies

Please or to participate in this conversation.