title: Search scope for multiple columns of data in Laravel Model
body: <p>I have a Model within Laravel called <code>Blah</code> (I named it this way because you can't simply name a class <code>Class</code>, so I changed the name temporarily).</p>
  
  <p>Within the <code>classes</code> table, each row contains data like this:</p>
  
  <pre><code>   {
         year: "2015",
         term: "Summer",
         subject_code: "DIGM",
         course_no: "350",
         instr_type: "Lecture",
         instr_method: "Face To Face",
         section: "003",
         crn: "42953",
         course_title: "Digital Storytelling",
         credits: "3.0",
         day: "R",
         time: "06:30 pm - 09:20 pm",
         instructor: "Teacher Name",
         campus: "University Building",
         max_enroll: "18",
         enroll: "18",
         building: "PLACE",
         room: null,
         description: "By surfing the internet and playing computer games, by lectures, assigned readings, class screening, and research projects, this class explores the impact of digital media on art, design and daily living. This is a writing intensive course. ",
         pre_reqs: "",
         co_reqs: ""
     }
  </code></pre>
  
  <p>I developed a scope search query within my <code>Blah</code> Model like so:</p>
  
  <pre><code>/**
   * Search scope query
   *
   * @param string
   * @return QueryBuilder
   */
  public function scopeSearch($query, $searchTerm) {
      return $query
          -&gt;where('course_title', 'like', "%" . $searchTerm . "%");
  }
  </code></pre>
  
  <p>The above scope search would allow me to search for the term <code>digital</code> and it would return the class called <code>Digital Storytelling</code>. </p>
  
  <p>However, I would like to take it further - I would like to be able to, in the same search scope, perform a search like <code>DIGM 350</code> (these two data points are from <code>subject_code</code> and <code>course_no</code>) and it would bring up the same result. </p>
  
  <p>So, searching <code>DIGM 350</code> and/or <code>digital</code> would bring up the same result. This would be part of my site's auto-search box.</p>
  
  <p>Final Query:</p>
  
  <pre><code>/**
   * Search for course title or subject name
   * @param $query
   * @param $searchTerm Course Title or Subject Name
   * @return mixed
   */
  public function scopeSearch($query, $searchTerm) {
      return $query
          -&gt;where('course_title', 'like', '%' . $searchTerm . '%')
          -&gt;orWhere(DB::raw("subject_code || ' ' ||  course_no"),
              'like',
              '%' . $searchTerm . '%'
          )
          ;
  }
  </code></pre>
  
  <p>Thanks Ian.</p>