【问题标题】:Paginating in Laravel在 Laravel 中进行分页
【发布时间】:2020-03-07 04:14:56
【问题描述】:

我正在为 Uni 完成一项任务,但是在我的 cmets 字段分页时遇到了障碍。我已经把我的控制器、模型和视图放在下面(按顺序)

<?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--}}

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

有人知道这是为什么吗?

任何帮助将不胜感激:D

2019 年 13 月 11 日 17:44 更新

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

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

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

【问题讨论】:

    标签: php laravel laravel-blade pagination


    【解决方案1】:

    改变 {{ $cmets -> links() }} 并试试这个

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

    【讨论】:

    • 感谢您的回复我已经尝试了您的建议,但仍然遇到同样的错误。
    【解决方案2】:

    我建议这样分页。

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

    还有,改变

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

    【讨论】:

    • 我尝试了你的建议,但我仍然得到与以前相同的错误。
    • 很傻但是为什么会有空格?用这个替换你的函数:``` public function index() { $cmets = Comment::paginate(self::COMMENTS_PER_PAGE);返回视图 ('index')->with(['cmets' => $cmets]); } ```
    【解决方案3】:
     {!! $comments->appends(Request::except('page'))->render() !!}
    

    【讨论】:

    • 没有任何解释的代码很少有帮助。 Stack Overflow 是关于学习的,而不是提供 sn-ps 来盲目复制和粘贴。请编辑您的问题并解释它如何回答所提出的具体问题。见How to Answer
    猜你喜欢
    • 2014-05-08
    • 1970-01-01
    • 2021-09-27
    • 2019-11-04
    • 2021-02-10
    • 2018-09-04
    • 2019-10-02
    • 2014-09-01
    • 1970-01-01
    相关资源
    最近更新 更多