0

I got an error related to the pagination of read notifications. Thank you for your help. This is my controller code that gives this error when it runs

  public function NotificationsRead(User $user)
{


    $no = Auth::user()->readNotifications()->paginate(7)->latest()->get();
    return view('user-profile.notifications-read', compact('user', 'no'));
}

And when I delete latest(), he gives an error to get() I do not know how to paginate. and A have two types notifications

2 Answers2

0

You should do

$no = Auth::user()->readNotifications()->latest()->paginate(7);

In this way you are ordering descending before pagination. Your code orders the result collection created by pagination.

https://laravel.com/docs/8.x/queries#latest-oldest

Jack
  • 489
  • 1
  • 6
  • 18
  • Thank you I did this, only the pagination button is created and all the constant duplicate notifications are repeated on each page. – user16426192 Jan 08 '22 at 09:47
0

@jack this is my Blade

  <div class="card-header text-black text-bold">
            اعلان ها
        </div>
        <div class="card-body">
            <blockquote class="blockquote mb-0">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">موضوع:</h5>
                        @foreach(auth()->user()->readnotifications->where('type','App\Notifications\NewReplySubmittedRoyal') as $notification)
                            <p class="card-text"><a
                                    href="{{$notification->data['UserProfile']}}">{{$notification->data['name']}}</a>
                                یک پاسخ برای پرسش شما با محتوای:
                                {{$notification->data['thread_title']}}
                                در {{$notification->data['time']}}<br>ثبت کرد.
                            </p>
                            <form>

                                <button type="submit" class=" btn btn-outline-success mb-2 ml-2"><a
                                        href="{{$notification->data['route']}}">لینک پرسش</a></button>
                            </form>
                            <hr>
                        @endforeach
                        {{--*********** نوع دوم نوتیفیکیشن--}}
                        @foreach (auth()->user()->readNotifications->where('type','App\Notifications\FollowerNotifications') as $notification)
                            <p class="card-text">{{$notification->data['name']}} یک پرسش با محتوای:
                                {{$notification->data['thread_title']}}
                                ایجاد کرد.
                            </p>

                            <a href="{{$notification->data['route']}}">لینک پرسش</a>
                            <hr>
                        @endforeach
                        {{$no->render()}}

I have two types notifications

  • I can't see where pagination is used. Anyway see https://laravel.com/docs/8.x/pagination – Jack Jan 08 '22 at 11:19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 08 '22 at 15:29