首页 > 解决方案 > 在 Laravel 中进行分页

问题描述

我正在为 Uni 做一项任务,但是我在评论字段中遇到了一个障碍。我已经把我的控制器、模型和视图放在下面(按顺序)

<?php

namespace App\Http\Controllers;

use App\Comment;
use Illuminate\Http\Request;

class CommentController extends Controller
{
    const COMMENTS_PER_PAGE = 5;

    public function index () {
        $comments = Comment::paginate (self::COMMENTS_PER_PAGE);
        return view ('index') -> with (['comments' => $comments]);
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        //
    }

    public function show(Comment $comment)
    {
        //
    }

    public function edit(Comment $comment)
    {
        //
    }

    public function update(Request $request, Comment $comment)
    {
        //
    }

    public function destroy(Comment $comment)
    {
        //
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{

    public static function paginate(int $COMMENTS_PER_PAGE)
    {
    }
}

{{--@extends('layout.master)--}}
{{--@section('content')--}}
    <div class="container main-table">
        <div class="box">

            <h1 class="title">Guestbook Comments</h1>

            @if (count ($comments) > 0)
                <table class="table is-striped is-hoverable">
                    <thead>
                    <tr>
                        <th>User</th>
                        <th>Comment</th>
                        <th>Date</th>
                        <th>Likes</th>
                    </tr>
                    </thead>
                    <tbody>

                    @foreach ($comments as $c)
{{--declaring the comments--}}
                        <tr>
                            <td>{{ $c -> name }}</td>
                            <td>{{ $c -> comment }}</td>
                            <td>{{ $c -> created_at -> format ('D jS F') }}</td>
                            <td>{{ $c -> likes }}</td>
                        </tr>

                    @endforeach

                    </tbody>
                </table>
{{--looking at pagination--}}
                {{ $comments -> links() }}
                        @else
                <div class="notification is-info">
                    <p>
                        The Guestbook is empty. Why not add a comment?
                    </p>
                </div>
            @endif
        </div>

    </div>
{{--@endsection--}}

我设法让它在更改为分页之前显示评论。这样做之后,我遇到了让它显示的问题。我没有收到错误,我只是得到一个空白页,好像没有任何问题。它不会显示任何不在我的评论表中的标题或文本。

有没有人知道为什么会这样?

任何帮助将不胜感激:D

更新 17:44 13/11/19

刚刚意识到链接到了错误的页面,但刚刚发现 count 函数有问题,因为它说没有相关的 ARRAY ("Facade\Ignition\Exceptions\ViewException count(): Parameter must be an实现 Countable 的数组或对象(查看:C:\XAMPP\htdocs\Assignment\resources\views\comment\comments.blade.php)“)

有人知道为什么吗?我认为它只会逐个列出评论,直到读出 5 条评论。

有想法该怎么解决这个吗?:)

标签: phplaravellaravel-bladepagination

解决方案


我建议这样分页。

public function index () {
   const COMMENTS_PER_PAGE = 5;
    $comments = Comment::paginate(self::COMMENTS_PER_PAGE);
    return view('index', compact('comments'))
        ->with('i', ($request->input('page', 1) - 1) * 5);


}

还有,改变

{!!$comments->links() !!} 到 {!! $comments->render() !!}


推荐阅读