Last active
November 28, 2017 06:36
-
-
Save cyberfly/34d481882230421ae5e02cb445aa864b to your computer and use it in GitHub Desktop.
Laravel Search With Pagination
This file contains hidden or 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
// search form here | |
<form action="/posts/index" method="GET" class="form-vertical"> | |
<label>Title</label> | |
<input type="text" name="search_title" class="form-control" value="{{ Request::get('search_title') }}"> | |
<label>Description</label> | |
<input type="text" name="search_description" class="form-control" value="{{ Request::get('search_description') }}"> | |
<label>User Id</label> | |
<input type="text" name="search_user" class="form-control" value="{{ Request::get('search_user') }}"> | |
<button type="submit" class="btn btn-primary">Search</button> | |
</form> | |
//listing table below | |
<table> | |
<tr> | |
<td></td> | |
<td></td> | |
<tr> | |
</table> | |
{{ $posts->appends(Request::except('page'))->links() }} |
This file contains hidden or 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
<?php | |
public function index(Request $request) { | |
$posts = new Post; | |
//if have relationship, use this code below to initialize Post | |
//$posts = Post::with('user','category'); | |
//kalau ada search by title, filter posts by title | |
if (!empty($request->search_title)) { | |
$posts = $posts->whereTitle($request->search_title); | |
} | |
//kalau ada search by description | |
if (!empty($request->search_description)) { | |
$posts = $posts->where('story','LIKE',"%$request->search_description%"); | |
} | |
//kalau ada search by user id | |
if (!empty($request->search_user)) { | |
$posts = $posts->whereUserId($request->search_user); | |
} | |
//kalau buat all in one search box | |
if (!empty($request->search_anything)) | |
{ | |
$search_anything = $request->search_anything; | |
$posts = $posts->where(function ($query) use ($search_anything) { | |
$query->orWhere('title', $search_anything) | |
->orWhere('story', 'like' ,'%'.$search_anything.'%') | |
->orWhereHas('author', function ($query) use ($search_anything) { | |
$query->where('author_email', $search_anything); | |
}); | |
}); | |
} | |
//kalau search by relationship condition | |
if (!empty($request->search_category)) { | |
$search_category = $request->search_category; | |
$posts = $posts->whereHas('subcategory', function ($query) use ($search_category) { | |
$query->where('category_id', $search_category); | |
}); | |
} | |
//kalau buat search by start date | |
if (!empty($request->start_date)) | |
{ | |
$start_date = $request->start_date; | |
$posts = $posts->whereDate('created_at','>=',$start_date); | |
} | |
//if user search by end date | |
if (!empty($request->end_date)) | |
{ | |
$end_date = $request->end_date; | |
$posts = $posts->whereDate('created_at','<=',$end_date); | |
} | |
$posts = $posts->orderBy('id','desc')->paginate(3); | |
return view('posts.index',compact('posts')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment