【问题标题】:Laravel 7 Search Bar using eloquent not displaying resultLaravel 7搜索栏使用雄辩不显示结果
【发布时间】: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

标签: php laravel eloquent


【解决方案1】:

不需要在 eloquent 模型类中定义table('posts'),因为模型总是将表名设置为类名(复数形式)。还有一件事,当未设置搜索查询参数时,无需应用 where 条件。如果在搜索变量中分配了任何值,则应使用 where 条件。

请这样做。

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'));
}

【讨论】:

  • 不需要在 eloquent 模型类中定义表('posts'),因为模型总是将表名设置为类名(复数形式)。再想一想,当没有设置搜索查询参数时,不需要应用 where 条件,如果在搜索变量中分配了任何值,则应该使用 where 条件。
  • 它现在正在使用上面的解决方案来完成这项工作
【解决方案2】:

您在 Blade 迭代中的主要问题。请将 count($posts) 替换为 $posts->total() 方法。因为 $posts 是分页数据对象而不是数组列表。

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 2021-05-12
    • 2021-01-25
    • 2020-04-26
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多