Kashif Shahzad
Kashif Shahzad

Reputation: 135

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

Upvotes: 0

Views: 291

Answers (3)

Ishfaq Rahim
Ishfaq Rahim

Reputation: 11

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

Upvotes: 1

Kashif Shahzad
Kashif Shahzad

Reputation: 135

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

Upvotes: 0

SEYED BABAK ASHRAFI
SEYED BABAK ASHRAFI

Reputation: 4271

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

Upvotes: 0

Related Questions