【发布时间】:2021-11-24 16:31:31
【问题描述】:
我正在使用 laravel 7,我正在尝试在我的项目中实现搜索。 这是我的控制器搜索功能代码
public function search(Request $request)
{
$search = Request::get('search');
$posts = Post::table('posts')
->where('title', 'like', '%' . $search . '%')
->paginate(5);
return view('dashboard', ['posts' => $posts]);
}
这是路线
Route::get('/search', 'PostsController@search');
这是我的搜索条形码
<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>
这是仪表板刀片中的表格
@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
我的桌子是这样的 当我按下搜索按钮时,我希望它看起来像这样 我不知道为什么搜索没有在数据表上显示任何内容似乎是什么问题。请告诉我如何解决这个问题。谢谢
【问题讨论】:
-
为什么是 Post::table('posts')->where,就是 Post::where,请试一试
-
我以前试过,但还是一样。搜索没有结果。
-
请您说明您尝试显示它的位置/方式。
-
@Rwd 我编辑了帖子。
-
对不起,我的意思是你能展示你如何在代码中使用它,即你试图使用的刀片文件
$posts。