【发布时间】: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