Skip to content

Instantly share code, notes, and snippets.

@ryanorsinger
Created October 14, 2014 20:08
Show Gist options
  • Save ryanorsinger/f5989a7190494f90f234 to your computer and use it in GitHub Desktop.
Save ryanorsinger/f5989a7190494f90f234 to your computer and use it in GitHub Desktop.
query, search, and pagination in PostsController@index
public function index()
{
$search = Input::get('search');
$query = Post::orderBy('created_at', 'desc');
if (is_null($search)) {
$posts = $query->paginate(3);
} else {
$posts = $query->where('title', 'LIKE', "%{$search}%")
->orWhere('body', 'LIKE', "%{$search}%")
->paginate(3);
}
return View::make('posts.index')->with('posts', $posts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment