php - Laravel Query Builder get only item with specific progress
Get the solution ↓↓↓Good day, I'm trying to get orders with products that have a specific products progress.
My Laravel query looks like this:
return Order::whereHas('products.progress', function (Builder $query) {
$query->where('progress_id', 100)
->orWhereBetween('progress_id', [160, 240]);
})
->with('products.progress')
->select('orders.id', 'orders.customer_id', 'orders.created_at')
->get();
In the query I set a where and whereBetween, the last Product has the progress_id 50 that one should not be loaded into the order.
How to do it? Thank you for reading.
Answer
Solution:
i think you want to Constraining Eager Loads
so your (wheres) should be on the loading ...
return Order::with(['products'=>function($query){
$query->whereHas('progress',function ($query){
$query->where('progress_id', 100)
->orWhereBetween('progress_id', [160, 240]);
})->with('progress');
}])
->select('orders.id', 'orders.customer_id', 'orders.created_at')
->get();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreign key constraint is incorrectly formed laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.