title: Method Illuminate\Database\Query\Builder::filter does not exist
body: <p>I am following a tutorial to write 2 classes for filtering threads in a forum application.I got this error in line</p>
  <pre><code> $threads = Thread::latest()-&gt;filter($filters); // in threadscontroller
  </code></pre>
  <p><strong>Error</strong>:</p>
  <blockquote>
  <p>Method Illuminate\Database\Query\Builder::filter does not exist.</p>
  </blockquote>
  <p><strong>ThreadsController</strong> with index method:</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Thread;
  use App\Channel;
  use App\Filters\ThreadFilters;
  
  use Illuminate\Http\Request;
  
  class ThreadsController extends Controller
  {
  
      public function __construct(){
  
          $this-&gt;middleware('auth')-&gt;only('store','create');
  
      }
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index(Channel $channel,ThreadFilters $filters)  
      {
  
  $threads = Thread::latest()-&gt;filter($filters);  
  
  if($channel-&gt;exist){
      $threads-&gt;where('channel_id',$channel-&gt;id);
  }
  
  $threads = $threads-&gt;get();
  
  return view('threads.index',compact('threads'));
  
  
       
      }
  </code></pre>
  <p>This is the <strong>abstract class Filters</strong>:</p>
  <pre><code>&lt;?php
  
  namespace App\Filters;
  use Illuminate\Http\Request;
  
  abstract class Filters{
  
  protected $request;
  protected $builder;
  
  protected $filters = [];
  
      public function __construct(Request $request){
          $this-&gt;request = $request;
  
  
  
      }
  
      public function apply($builder){
          $this-&gt;builder = $builder;
  
          foreach($this-&gt;getFilters() as $filter=&gt;$value){   //filter by,value yunus mesela.
  if(method_exist($this,$filter)){    
      $this-&gt;$filter($value);  
  }
  
          }
  
          return $this-&gt;builder;
  
      }
  
  
      public function getFilters(){
  
          return $this-&gt;request-&gt;intersect($this-&gt;filters); 
      } 
  
  }
  </code></pre>
  <p>Here <strong>ThreadFilters.php</strong> which extends filters class:</p>
  <pre><code>&lt;?php
  
  namespace App\Filters;
  
  use App\User;
  
  use Illuminate\Http\Request;
  
  
  class ThreadFilters extends Filters
  {
  protected $filters =['by'];        
  
  protected function by($username){
      $user = User::where('name',$username)-&gt;firstorFail();
  
      return $this-&gt;builder-&gt;where('user_id',$user-&gt;id);          
  }
  
  }
  </code></pre>
  <p>If I change latest to all, I get this <strong>error</strong>:</p>
  <blockquote>
  <p>Type error: Argument 1 passed to
  Illuminate\Support\Collection::filter() must be callable or null,
  object given, called in</p>
  </blockquote>
  <p>Also can anyone explain me what is $builder doing in those classes?</p>