Laravel Eloquent 关系无效

Laravel Eloquent relationships With does not work

我正在尝试此代码,但在 whith where query 中不起作用 谁能解决这个问题?

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();

您应该使用 where(),然后再使用 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();

经过大量搜索,我已经通过使用 WhereHas 闭包 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();```
$posts = App\User::whereHas('posts', function (Builder $query) {
    $query->where('name', 'like', `foo%');
})->get();