Home > Software design >  how to paginate read notifications in Laravel
how to paginate read notifications in Laravel

Time:01-09

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

CodePudding user response:

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

CodePudding user response:

@jack this is my Blade

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

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

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

I have two types notifications

  •  Tags:  
  • Related