Im using laravel 7 and I'm trying to implement search in my project. this is my controller search function code
public function search(Request $request)
{
$search = Request::get('search');
$posts = Post::table('posts')
->where('title', 'like', '%' . $search . '%')
->paginate(5);
return view('dashboard', ['posts' => $posts]);
}
Here is the route
Route::get('/search', 'PostsController@search');
here is my search bar code
<form action="/search" method="get">
<div class="input-group">
<input type="search" name="search" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Search</button>
</span>
</div>
</form>
Here is the table in dashboard blade
@if(count($posts) > 0)
<table class="table table-bordered">
<tr>
<thead class="table-dark">
<th>Entry#</th>
<th>Consignee</th>
<th>Description</th>
<th>Uploaded By</th>
<th>Date Uploaded</th>
<th>Actions</th>
<th>Status</th>
</thead>
</tr>
@foreach ($posts as $post)
<tbody id="myTable" name="myTable">
<tr>
<td>{{$post->title}}</td>
<td>{{$post->consignee}}</a></td>
<td>{!!$post->body!!}</td>
<td>{{$post->user->name}}</td>
<td>{{$post->created_at}}</td>
<td>
<a href="/posts/{{$post->id}}/" class="btn btn-primary">View</a>
<button type="button" class="btn btn-danger" data-toggle="modal" href="#deletePost{{$post->id}}">
Delete
</button>
</td>
<td>{{$post->status}}</td>
</tr>
@endforeach
</tbody>
</table>
{{$posts->links()}}
@else
<p> You have no entries</p>
@endif
My table looks like this I want it to look like this when I press the search button I don't know what seems to be the problem why the search is not displaying anything on the data table. Please show me how can I fix this. Thanks
CodePudding user response:
No need to define table('posts')
in eloquent model class because model always set table name its class name (plural form). One more thing, when search query parameter is not set there's no need to apply where condition. If any value assign in search variable then it should be use where condition.
Please do it.
public function search (Request $request)
{
$search = $request->get('search');
$posts = Post::when($search, function($sql) use ($search) {
$sql->where('title', 'like', '%' . $search . '%');
})
->paginate(5);
return view('dashboard', compact('posts'));
}
CodePudding user response:
Your main problem in Blade iteration. Kindly replace count($posts) with $posts->total() method. Because $posts are paginated data object not array list.
@if($posts->total() > 0)
<table class="table table-bordered">
<tr>
<thead class="table-dark">
<th>Entry#</th>
<th>Consignee</th>
<th>Description</th>
<th>Uploaded By</th>
<th>Date Uploaded</th>
<th>Actions</th>
<th>Status</th>
</thead>
</tr>
<tbody id="myTable" name="myTable">
@foreach ($posts as $post)
<tr>
<td>{{$post->title}}</td>
<td>{{$post->consignee}}</a></td>
<td>{!!$post->body!!}</td>
<td>{{$post->user->name}}</td>
<td>{{$post->created_at}}</td>
<td>
<a href="/posts/{{$post->id}}/" class="btn btn-primary">View</a>
<button type="button" class="btn btn-danger" data-toggle="modal" href="#deletePost{{$post->id}}">
Delete
</button>
</td>
<td>{{$post->status}}</td>
</tr>
@endforeach
</tbody>
</table>
{{$posts->links()}}
@else
<p> You have no entries</p>
@endif