title: Filtering data in laravel and handling the views
body: <p>I have a tv show netflix-esque project I'm building where I have a <code>Shows</code> page which I want to filter on format. Each show contains episodes which can have a tv, dvd and bd format. </p>
  
  <p>Currently I'm filtering using separate routes and controllers which extend the base <code>ShowsController</code>.</p>
  
  <pre><code>Route::get('shows/view/{type}', ['as' =&gt; 'shows.viewtype', 'uses' =&gt; 'ShowsController@viewType',]);
  Route::get('shows/bluray',['as' =&gt; 'shows.bluray','uses' =&gt; 'ShowsBlurayController@index']);
  Route::get('shows/dvd',['as' =&gt; 'shows.dvd','uses' =&gt; 'ShowsDVDController@index']);
  Route::get('shows/tv',['as' =&gt; 'shows.tv','uses' =&gt; 'ShowsTVController@index']);
  </code></pre>
  
  <p>Example of one of the format controllers</p>
  
  <pre><code>class ShowsBlurayController extends ShowsController
  {
      public function index()
      {
          // Set user state for browsing bluray
          Session::push('user.showtype', 'bluray');
  
          $shows = $this-&gt;show-&gt;getBlurayPaginated(16);
  
          return $this-&gt;getIndexView(compact('shows'));
      }
  }
  </code></pre>
  
  <p>I use the <code>getIndexView()</code> method (in the <code>ShowsController</code>) to determine one of 2 available views: <code>poster</code> and <code>list</code>.</p>
  
  <pre><code>public function getIndexView($shows)
  {
      $viewType = get_session_or_cookie('show_viewtype', 'list');
      if ($viewType == 'posters') {
          return View::make('shows.index', $shows)
              -&gt;nest('showsView', 'shows.partials.posters', $shows);
      } else {
          return View::make('shows.index', $shows)
              -&gt;nest('showsView', 'shows.partials.list', $shows);
      }
  }
  </code></pre>
  
  <p>The shows are filtered based on the episodes:</p>
  
  <pre><code>public function getBlurayPaginated($perPage)
  {
      return $this-&gt;getByFormat('BD')-&gt;with('tagged')-&gt;paginate($perPage);
  }
  private function getByFormat($format)
  {
      return $this-&gt;show-&gt;whereHas('episodes', function ($q) use ($format) {
          $q-&gt;whereHas('format', function ($q) use ($format) {
              $q-&gt;where('format', '=', $format);
          });
      });
  }
  </code></pre>
  
  <p>The problem is that I want to do this in a clean way. When a user selects a format, that filter will be applied. Currently all of this is kind of scattered across controllers and doesn't quite make sense.</p>
  
  <p>I also thought of doing something like this in the <code>routes.php</code>:</p>
  
  <pre><code>Route::get('shows/format/{format}',['as' =&gt; 'shows.format','uses' =&gt; 'ShowsController@index']);
  </code></pre>
  
  <p>And then handle the filtering in the index, but that also seems a weird place to do that.</p>
  
  <p>This approach does work, but I don't want to screw myself later on with it. I'm planning a simple search which should take the filter into account.</p>
  
  <p>In other words, how can I organize the code in such a way that getting data from the database will take the filter into account which has been set? (Session states maybe?)</p>