jeudi 2 juillet 2020

Laravel - conditional filtering with relation table

I have to conditionally filter results in the below query, based on $search is present or not. Below is my query,

    Sellers::with(
        'shop:id,width,height,business_id'
    )->with(
        'unit.location:id,name as l_name'
    )->select(
        'sellers.id', 'sellers.name' , 'address'
    )->when($search, function ($query, $search) {
         $query->where('l_name', 'like', '%' . $search . '%');
    })
    ->findOrFail($request->seller_id);

The relations are,

Sellers has many units. Units has one location.

I need to filter the above result for location names based on the availability of search param. I have tried adding aliases, but they are throwing errors.

How can this be attained?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire