Laravel 7 使用 eloquent 的搜索栏未显示结果

Laravel 7 Search Bar using eloquent not displaying result

我正在使用 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>

这是仪表板 blade

中的 table
@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

我的 table 看起来像这样 当我按下搜索按钮时,我希望它看起来像这样 我不知道搜索未显示任何数据的问题似乎是什么 table。请告诉我如何解决这个问题。谢谢

无需在 eloquent 模型 class 中定义 table('posts') 因为模型总是设置 table 命名其 class 名称(复数形式)。还有一件事,当未设置搜索查询参数时,无需应用 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'));
}

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