Laravel Eloquent relationships With does not work

I am trying this code but in whith where query does not work
any One can solve this problem?

Quotation::with(['QuoParts', 'client'=>function($cq){
            $cq->orWhere('first_name', 'LIKE', '%Muhammad%');
        }, 'user'])->orWhere(function($q) use ($s){
            $q->orWhere('sku', 'LIKE', '%'.$s.'%');
            $q->orWhere('issue_date', 'LIKE', '%'.$s.'%');
            
        })->take($length)->skip($r->start)->get();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

$posts = AppUser::whereHas('posts', function (Builder $query) {
    $query->where('name', 'like', `foo%');
})->get();

Method 2

You should use where(), then after that orWhere()

Quotation::with([
    'QuoParts', 'user', 'client' => function ($cq) {
        $cq->where('first_name', 'LIKE', '%Muhammad%');
    },
])->where(function ($q) use ($s) {
    $q->where('sku', 'LIKE', '%' . $s . '%');
    $q->orWhere('issue_date', 'LIKE', '%' . $s . '%');

})->take($length)->skip($r->start)->get();

Method 3

After lot of searching i have solved that by Using WhereHas closure function

$q = Quotation::with(['QuoParts', 'user', 'client'])->whereHas('client', function ($query) use ($s) {
            $query->where('sku', 'like', '%' . $s . '%');
            $query->orWhere('first_name', 'like', '%' . $s . '%');
            $query->orWhere('last_name', 'like', '%' . $s . '%');
        })->orWhereHas('user', function ($query) use ($s) {
            $query->where('name', 'like', '%' . $s . '%');
            
        })
            ->orWhere(function ($q) use ($s) {
                $q->orWhere('sku', 'LIKE', '%' . $s . '%');
                $q->orWhere('issue_date', 'LIKE', '%' . $s . '%');
            })
            ->take($length)
            ->skip($r->start)
            ->get();```


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x