N
N
NikSIk312019-10-14 15:58:19
Laravel
NikSIk31, 2019-10-14 15:58:19

Laravel query with with and where?

And how could one get data when: there is an order and it has many offers, you need to get all orders with the search status and if it has offers with user_id == Auth::id() . It seems like you can’t just do it through where and with

$orders = Order::where('status', 'search')
            ->with([
                'offers' => function ($q) {
                    $q->where('user_id', Auth()->id())->first();
                }
            ])->get();

order is required, and offers is either empty, or it contains one offer (required) . And it turns out that once again one where must be done separately with a check for non-null in offers for each order ??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vfreelance, 2019-10-14
@NikSIk31

google whereHas

G
Gennady, 2019-10-15
@GssGenic

I would use ::where(....)->joinLeft(....)->select('orders.*' , 'offers.* as offer')->get();
select is needed so that the id field of offers does not replace the id field of the orders table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question