/**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $startup = $request->segment(2);
     $startup = Startup::where('url', '=', $startup)->firstOrFail();
     if (false == Auth::user() or $startup->owner->id != Auth::user()->id) {
         return Redirect::route('startups.show', ['startup' => $startup->url]);
     }
     return $next($request);
 }
 /**
  * @param null $tag
  * @param null $needs
  * @param null $orderBy
  * @param int $perPage
  * @return \Illuminate\Pagination\Paginator
  */
 public function allActive($tag = null, $needs = null, $orderBy = null, $perPage = 12)
 {
     $results = Startup::where('published', '=', true)->with('owner')->with('needs')->with('tags')->with('ratings');
     if ($tag) {
         $results->join('needs', 'startups.id', '=', 'needs.startup_id')->join('startup_tag', 'startup_tag.startup_id', '=', 'startups.id')->join('need_tag', 'need_tag.need_id', '=', 'needs.id')->join('tags', function ($join) {
             $join->on('need_tag.tag_id', '=', 'tags.id')->orOn('startup_tag.tag_id', '=', 'tags.id');
         })->where('tags.name', '=', $tag)->select('startups.*');
     }
     if ($needs) {
         $results->whereHas('needs', function ($q) use($needs) {
             $q->where('needs.skill_id', '=', $needs);
         });
     }
     if ($orderBy) {
         $results->orderBy($orderBy);
     } else {
         $results->orderBy('id', 'DESC');
     }
     $paginatedResults = $results->paginate($perPage);
     if ($needs or $tag) {
         $paginatedResults->appends(['needs' => $needs, 'tag' => $tag]);
     }
     return $paginatedResults;
 }
 /**
  * @param UpdateStartup $request
  * @param $startup
  * @return mixed
  */
 public function update(UpdateStartup $request, $startup)
 {
     $startup = Startup::where('url', '=', $startup)->firstOrFail();
     $this->dispatch(new UpdateStartupCommand($startup, $request->all()));
     Flash::message('Startup updated successfully!');
     return Redirect::action('StartupController@show', $startup->url);
 }
 /**
  * Remove the specified resource from storage.
  * DELETE /membership/{id}
  *
  * @param  int  $startup
  * @return Response
  */
 public function destroy($startup)
 {
     $startup = Startup::where('url', '=', $startup)->firstOrFail();
     $this->dispatch(new CancelMembership(Auth::user(), $startup));
     return Redirect::route('startups.show', ['url' => $startup->url]);
 }