在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():参数必须是实现Countable的数组或对象(视图:C:\ XAMPP \ htdocs \ Assignment \ resources \ views \ comment \ comments.blade.php)“)

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

关于如何解决此问题的任何想法? :)

jialinjl 回答:在Laravel分页

更改{{$ comments-> links()}}并尝试

 {!! $comments->render() !!}
,

我建议像这样分页。

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()!!}

,
 {!! $comments->appends(Request::except('page'))->render() !!}
本文链接:https://www.f2er.com/3124030.html

大家都在问