3

I'm trying to implement basic pagination when retrieving notifications, but I get the following error.

Method Illuminate\Notifications\DatabaseNotificationCollection::paginate does not exist.

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);
    }
}
Shanediesel
  • 161
  • 2
  • 9
  • That type of `Collection` doesn't have a `paginate` method, however, you can use a [helper function](https://www.itsolutionstuff.com/post/laravel-6-paginate-with-collection-or-arrayexample.html) to achieve this. – Peppermintology Jul 04 '21 at 09:34
  • use ```$notifications = auth()->user()->unreadNotifications()->paginate(5);```. It will paginate if you have ```unreadNotifications``` relationship defined in modal – Rajesh Paudel Jul 04 '21 at 11:46
  • Method Illuminate\Support\Collection::links does not exist. it returns that and I do have links() in the view, so I'm confused now. I don't either have the relationsip defined in the model, how will that be exactly? – Shanediesel Jul 04 '21 at 11:58
  • How is your relationship defined in the User model? and how is the Scope defined? – Cornel Raiu Jul 05 '21 at 05:00
  • Does your User model use the `Notifiable` trait? Make sure the unreadNotifications() method returns a `\Illuminate\Database\Query\Builder` instance for paginate to work. – Cornel Raiu Jul 05 '21 at 05:12
  • @CornelRaiu The user model us the notifiable trait. There is no relationship with notifications in the user model, but notifications still work – Shanediesel Jul 05 '21 at 11:35

2 Answers2

2

You've to call paginate() on query builder instance not on a collection. Correct syntax will be :

$notifications = auth()->user()->unreadNotifications()->paginate(5);

Ahsan Zaman
  • 51
  • 2
  • 6
  • I tried that and it returns: Method Illuminate\Notifications\DatabaseNotificationCollection::paginate does not exist. – Shanediesel Jul 04 '21 at 11:30
  • Did you add the trait on your users model ? ```use Illuminate\Notifications\Notifiable;``` ```...``` ```use Notifiable``` Also you need to loop through ```$notifications->items()``` rather that ```foreach ($notifications as $notification)``` hope it helps. – Ahsan Zaman Jul 06 '21 at 10:45
1

You should paginate before looping through the collection, otherwise you will be retrieving all records matching the query, when you only need 5. Like this:

$messages = collect();
$notifications = auth()->user()->unreadNotifications()->paginate(5);
 
foreach($notifications as $notification) {
    $message = NotificationToMessageFactory::make($notification->type)->toMessage($notification->data);
    $messages->push($message);   
}
Nuno Peixoto
  • 144
  • 1
  • 10
  • I updated the post and it's returning a new error: Method Illuminate\Notifications\DatabaseNotificationCollection::paginate does not exist. – Shanediesel Jul 04 '21 at 11:09
  • I guess you should paginate before retrieving from the database so you don't pull all of them to just display 5 ... that wouldn't make sense. So instead of `user()->unreadNotifications->paginate(5)` you should do: `user()->unreadNotifications()->paginate(5)` to run the paginate on a query builder instance – Cornel Raiu Jul 05 '21 at 05:05