title: get laravel search with multi options
body: <p>I'm using laravel 5.4,</p>
  
  <p>I want to get my website search result with multi options, currently I have <code>title,category &amp; location</code> fields in my app and I want to let users get result by choosing any of these fields for example if one user just chose location base search be able to get the results without getting error of not filling other options etc.</p>
  
  <p>The Issue;
  Before I only worked on title field and everything worked just fine but when I define other two options <code>categories &amp; locations</code> results will not return anymore, where is my mistake?</p>
  
  <p><a href="https://i.stack.imgur.com/5WWPw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5WWPw.png" alt="screenshot"></a></p>
  
  <p>this is my controller:</p>
  
  <pre><code>public function search() {
          $q = Input::get('q');
          $ads = null;
          $categories = null;
          $locations = null;
          if($q) {
            $categories =  Category::where('name','LIKE','%'.$q.'%')
                -&gt;paginate(2)
                -&gt;appends('q', $q);
          }
          if($q) {
            $locations =  Location::where('name','LIKE','%'.$q.'%')
                -&gt;paginate(2)
                -&gt;appends('q', $q);
          }
          if($q) {
              $ads =  Ad::where('title','LIKE','%'.$q.'%')
                  -&gt;orWhere('description','LIKE','%'.$q.'%')
                  -&gt;paginate(2)
                  -&gt;appends('q', $q);
          }
          return view('search-result', compact('ads', 'categories', 'locations', 'q'));
       }
  </code></pre>
  
  <p>my route:</p>
  
  <pre><code>//search routes
  Route::any('/search', 'HomeController@search');
  </code></pre>
  
  <p>my view:</p>
  
  <pre><code>@extends('layouts.view')
  
  @section('content')
  
  @if(isset($ads))
  &lt;h2&gt;Search results&lt;/h2&gt;
  &lt;table class="table table-striped"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Email&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          @foreach($ads as $ad)
          &lt;tr&gt;
              &lt;td&gt;{{$ad-&gt;title}}&lt;/td&gt;
              &lt;td&gt;{{$ad-&gt;description}}&lt;/td&gt;
          &lt;/tr&gt;
          @endforeach
      &lt;/tbody&gt;
  &lt;/table&gt;
  @else
  no results!
  @endif
  @endsection
  </code></pre>
  
  <p>Update:</p>
  
  <p>My controller relationships:</p>
  
  <pre><code>use Illuminate\Http\Request;
  use App\Events\StudentAdded;
  use App\Ad;
  use App\Company;
  use App\Location;
  use App\Category;
  use Carbon\Carbon;
  use Illuminate\Support\Facades\Input;
  </code></pre>
  
  <p>Models:
  Locations model</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Location extends Model
  {
  
    protected $fillable = [
        'name',
    ];
  
    public function ads(){
       return $this-&gt;hasMany(Ad::class);
    }
  }
  </code></pre>
  
  <p>Category Model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Category extends Model
  {
  
    protected $fillable = ['name'];
  
    public function ads(){
       return $this-&gt;hasMany(Ad::class);
    }
  
  }
  </code></pre>
  
  <p>Ad model</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Ad extends Model
  {
  
      protected $fillable = [
          'company_id', 'title', 'slug', 'image', 'description', 'address', 'job_title', 'salary',
      ];
  
      public function company(){
         return $this-&gt;belongsTo(Company::class);
      }
      public function category(){
         return $this-&gt;belongsTo(Category::class);
      }
      public function location(){
         return $this-&gt;belongsTo(Location::class);
      }
      public function employment(){
         return $this-&gt;belongsTo(Employment::class);
      }
  }
  </code></pre>
  
  <p>Update 2:</p>
  
  <p>Search Form:</p>
  
  <pre><code>&lt;form class="form-inline myform" action="/search" method="POST" role="search"&gt;
              {{ csrf_field() }}
              &lt;div class="input-group" data-toggle="tooltip" data-placement="top" title="Title"&gt;
                &lt;span class="input-group-addon" id="basic-addon1"&gt;&lt;i class="fa fa-pencil"&gt;&lt;/i&gt;&lt;/span&gt;
                &lt;input name="q" type="text" class="form-control" placeholder="Title" aria-describedby="basic-addon1"&gt;
              &lt;/div&gt;
  
              &lt;label data-toggle="tooltip" data-placement="top" title="select category" class="sr-only mr-sm-2" for="inlineFormCustomSelect"&gt;Category&lt;/label&gt;
              &lt;select name="q" data-toggle="tooltip" data-placement="top" title="select category" class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect"&gt;
                @foreach ($categories as $category)
                &lt;option value="{{ $category-&gt;id }}"&gt;{{ $category-&gt;name }}&lt;/option&gt;
                @endforeach
              &lt;/select&gt;
  
              &lt;label data-toggle="tooltip" data-placement="top" title="select City" class="sr-only mr-sm-2" for="inlineFormCustomSelect"&gt;City&lt;/label&gt;
              &lt;select name="q" data-toggle="tooltip" data-placement="top" title="select City" class="custom-select mb-2 mr-sm-2 mb-sm-0" id="inlineFormCustomSelect"&gt;
                @foreach ($locations as $location)
                &lt;option value="{{ $location-&gt;id }}"&gt;{{ $location-&gt;name }}&lt;/option&gt;
                @endforeach
              &lt;/select&gt;
  
              &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
            &lt;/form&gt;
  </code></pre>