【发布时间】:2021-09-15 11:24:14
【问题描述】:
我在检索通知时尝试实现基本分页,但出现以下错误。
方法 Illuminate\Notifications\DatabaseNotificationCollection::paginate 确实 不存在。
public function index()
{
$messages = collect();
$notifications = auth()->user()->unreadNotifications->paginate(5);
foreach ($notifications as $notification) {
$message = NotificationToMessageFactory::make($notification->type)
->toMessage($notification->data);
$messages->push($message);
}
}
【问题讨论】:
-
那种
Collection没有paginate方法,但是你可以使用helper function来实现。 -
使用
$notifications = auth()->user()->unreadNotifications()->paginate(5);。如果您在 modal 中定义了unreadNotifications关系,它将分页 -
方法 Illuminate\Support\Collection::links 不存在。它返回它,我在视图中有 links() ,所以我现在很困惑。我也没有在模型中定义关系,那到底是什么?
-
你的关系在用户模型中是如何定义的? Scope是如何定义的?
-
您的 User 模型是否使用
Notifiable特征?确保 unreadNotifications() 方法返回一个\Illuminate\Database\Query\Builder实例以便分页工作。
标签: laravel laravel-pagination