lundi 23 novembre 2015

Use orWhere with 'whereHas' and 'with' in eloquent

I am trying to check a search value in a renationalized query to search if it matches with 3 different columns

    Table |  Column
-------------------
    Order     code
    Customer  name
    Customer  dni

I tried several ways but it just woks with the value name. This is what I have now:

$search = "%{$searchQuery}%";

$constraint = function ($query) use ($search){
    $query->where('name', 'like', $search)
          ->orWhere('dni', 'like', $search);
};
return Order::with(['customer' => $constraint])
                ->whereHas('customer', $constraint)
                ->orWhere('code', 'like', $search)
                ->paginate(15);

If I search by name it works, but if I put part of the dni it says

Trying to get property of non-object

I am accessing the value in the view like this:

{{ $order->customer->dni }}

or for example

{{ $order->code }}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire