How do I query based on data in another table in a related query?
There are 2 “divided order” data, I want to filter them by id.
Thanks in advance for your help
my code;
$data = CollectionOrder::with('dividedOrder','dividedOrder.orderDividedPiece','dividedOrder.orderDividedPiece.CollectionColorSizeBarcode')->find($request->order_id);
output;
You seem to be wanting to eager load a relationship but also constrain it such that it doesn’t load all divided_order
.
You can do that by passing a closure to with()
.
Docs: https://laravel.com/docs/11.x/eloquent-relationships#constraining-eager-loads
So in your case, it might look like:
$data = CollectionOrder::with([
'dividedOrder' => function (Builder $query) {
$query->whereIn('id', [1, 2, 3])
}
])
Same thing is true for lazy eager loading:
$collectionOrder = CollectionOrder::find($id);
$collectionOrder->load([
'dividedOrder' => function (Builder $query) {
$query->whereIn('id', [1, 2, 3])
}
]);
Docs: https://laravel.com/docs/11.x/eloquent-relationships#lazy-eager-loading
0