title: Use orWhere with 'whereHas' and 'with' in eloquent
body: <p>I am trying to check a search value in a renationalized query to search if it matches with 3 different columns </p>
  
  <pre><code>    Table |  Column
  -------------------
      Order     code
      Customer  name
      Customer  dni
  </code></pre>
  
  <p>I tried several ways but it just woks with the value name.
  This is what I have now:</p>
  
  <pre><code>$search = "%{$searchQuery}%";
  
  $constraint = function ($query) use ($search){
      $query-&gt;where('name', 'like', $search)
            -&gt;orWhere('dni', 'like', $search);
  };
  return Order::with(['customer' =&gt; $constraint])
                  -&gt;whereHas('customer', $constraint)
                  -&gt;orWhere('code', 'like', $search)
                  -&gt;paginate(15);
  </code></pre>
  
  <p>If I search by name it works, but if I put part of the dni it says </p>
  
  <pre><code>Trying to get property of non-object
  </code></pre>
  
  <p>I am accessing the value in the view like this:</p>
  
  <pre><code>{{ $order-&gt;customer-&gt;dni }}
  </code></pre>
  
  <p>or for example</p>
  
  <pre><code>{{ $order-&gt;code }}
  </code></pre>