Laravel multiple where condition in whereHas callback
<p>I am creating a general search function with Laravel 5.2 and I want to display all the books in which occurs the searched keyword in: the book's title, book's subject, book's plot, book's author name, book's author surname;
I thought that this code would work:</p>

<pre><code>$results = Book::whereHas('author', function ($query) use ($keyword)
                     {
                          $query-&gt;where('surname', 'LIKE', '%'.$keyword.'%')
                               -&gt;orWhere('name', 'LIKE', '%'.$keyword.'%');
                     })
                     -&gt;orWhere('title', 'LIKE', '%'.$keyword.'%')
                     -&gt;orWhere('plot', 'LIKE', '%'.$keyword.'%')
                     -&gt;orWhere('subject', 'LIKE', '%'.$keyword.'%')
                     -&gt;get();
</code></pre>

<p>But when I use as a keyword the name of an author, I get as a results the whole library.
Instead if I enter the surname of an author it works perfectly.</p>

<p>I found out this solution, that is not optimal in my opinion, but at least it works:</p>

<pre><code>$results = Book::whereHas('author', function ($query) use ($keyword)
                     {
                          $query-&gt;where('surname', 'LIKE', '%'.$keyword.'%');
                     })
                     -&gt;orWhereHas('author', function ($query) use ($keyword)
                     {
                          $query-&gt;where('name', 'LIKE', '%'.$keyword.'%');
                     })
                     -&gt;orWhere('title', 'LIKE', '%'.$keyword.'%')
                     -&gt;orWhere('plot', 'LIKE', '%'.$keyword.'%')
                     -&gt;orWhere('subject', 'LIKE', '%'.$keyword.'%')
                     -&gt;get();
</code></pre>

<p>Any suggestions?
Thank you in advance for your help!</p>