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

raulmercadox's avatar

Query with filter on child tables

I have these tables:

orders
----------
id (PK)
stage_id (FK)

stages
---------
id (PK)
phase

I'd like to query the orders which associated stage has a phase='2'

Thanks!

0 likes
3 replies
piljac1's avatar
piljac1
Best Answer
Level 28

@raulmercadox Not sure this is what you meant but try this and let me know (you must have a belongsTo stage relationship defined in your Order model for this to work) :

Order::whereHas('stage', function ($query) {
    $query->where('phase', 2);
})->get();

If phase is a string, just wrap the 2 in single quotes.

1 like
piljac1's avatar

@raulmercadox Great ! Please consider marking it as best answer if it solved your problem to close the topic :)

Please or to participate in this conversation.