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

vincent15000's avatar

Query with relationship and limit

Hello,

For example I have 3 types and I need to retrieve 4 productions of each type, so 12 productions at all.

$types = Type::with(['productions' => function ($query) {
    $query
        ->where('published', true)
        ->orderBy('creation_date', 'desc')
        ->orderBy('created_at', 'desc')
        ->limit(4);
}])->orderBy('order')->get();

But it retrieves me only 4 productions.

Why ? I don't understand ... what's wrong in my code ?

Thanks a lot for your help ;).

Vincent

0 likes
2 replies

Please or to participate in this conversation.