title: Search by joining two tables in laravel
body: <p>I am searching the jobs with <code>title</code> successfully from the <code>jobs_table</code>. It has <code>id, title, city_id, created_at</code>. I have another table naming <code>cities</code> having <code>id, name</code>. I just want to add both the tables and search by <code>name</code> of the city also.</p>
  <p>Job Model query:</p>
  <pre><code>public static function scopeSearch($query, $searchTerm)
  {
     return $query-&gt;where('job_title', 'like', '%' .$searchTerm. '%');
  }
  </code></pre>
  <p>Controller Query :</p>
  <pre><code>public function indexsearch(Request $request) 
  {
    $searchTerm = $request-&gt;input('searchTerm');
    $posts = Job::search($searchTerm)-&gt;orderBy('created_at', 'desc')-&gt;where('job_status', '0')-&gt;paginate(10);
    return view('front.pages.job.jobresult', compact('posts', 'searchTerm'))-&gt;with(['title'=&gt;'Result']);
  }
  </code></pre>
  <p>Blade Query :</p>
  <pre><code>&lt;form action=&quot;{{ route('job.jobresult') }}&quot; method=&quot;GET&quot; id=&quot;search-form_3&quot; class=&quot;mb-4&quot;&gt;
    {{csrf_field()}}
    &lt;div class=&quot;input-group&quot;&gt;
        &lt;input type=&quot;text&quot; class=&quot;search_3&quot; name=&quot;searchTerm&quot; placeholder=&quot;Search for Jobs...&quot; value=&quot;{{ isset($searchTerm) ? $searchTerm : '' }}&quot;&gt;
        &lt;input type=&quot;submit&quot; class=&quot;submit_3&quot; value=&quot;Search&quot; /&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>Till now everything is fine with single table search, Now I just want to join both tables and then search by <code>name</code> of the city also.</p>
  <p>Please help me out. Thanks in advance</p>
  <p>Job Model :</p>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
      class Job extends Model
      {
          protected $fillable = [
              'job_title','city_id'
          ];
      
      
          public static function scopeSearch($query, $searchTerm)
          {
              return $query-&gt;where('job_title', 'like', '%' .$searchTerm. '%');
          }
      }
  </code></pre>
  <p>City Model :</p>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class City extends Model
  {
      protected $table = 'cities';
  
      protected $fillable = [
          'name'
      ];
  }
  </code></pre>
  <p>I have tried :</p>
  <pre><code>return $query-&gt;where('cities.city', 'LIKE', '%' . $searchTerm . '%')-&gt;join('cities', 'jobs.city', '=', 'cities.id');
  </code></pre>
  <p>its working but how can I add another fields in it.</p>