Home > Blockchain >  laravel eloquent use `whereHas` and `orWhereHas` with Fixed where
laravel eloquent use `whereHas` and `orWhereHas` with Fixed where

Time:07-21

I have a query:

$undeliveredTransactions = Transaction::whereHas('carts.acceptedcarts', function ($query) {
        $query->where('delivered', false);
    })->orWhereHas('carts' , function ($query){
        $query->where('full_accepted' , false);
    })->where('user_id', Auth::id())->get();

The where('user_id' , Auth::id()) is fixed and necessary, but two whereHas one of them is necessary.

How can I implement this?

CodePudding user response:

This should work, using an Eloquent subquery

$undeliveredTransactions = 
    Transaction::where('user_id', Auth::id()
        ->where(function ($q) {
            $q->whereHas('carts.acceptedcarts', function ($query) {
                $query->where('delivered', false);
            })->orWhereHas('carts' , function ($query){
                $query->where('full_accepted' , false);
            });
        })->get();

CodePudding user response:

you can also use whereRelation, so your query is:

$undeliveredTransactions = Transaction::where('user_id', Auth::id())
->whereRelation('carts.acceptedcarts', 'delivered', false)
->orWhereRelation('carts' , 'full_accepted' , false)->get();
  • Related