Created
October 14, 2014 20:08
-
-
Save ryanorsinger/f5989a7190494f90f234 to your computer and use it in GitHub Desktop.
query, search, and pagination in PostsController@index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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