mvnobrega's avatar

Link lastPage () pagination URL

I am working in a discussion forum and when clicking on the topic the user should be redirected to the last URL of $ post-> links ()

I'm having a hard time solving this problem. The topics page looks like this:

 @foreach($rels->sortBy('created_at') as $r)
{...}

<div class="title"><a href="{{ route('relatoUser', [$r->grupo->slug, $r->slug] ) }}">{{$r->titulo}}</a>

{...}
@endforeach

I am thinking of doing something along these lines:

<div class="title"><a href="{{ route('relatoUser', [$r->grupo->slug, $r->slug] ) }}?page={{$r->comments->lastpage()}}">{{$r->titulo}}</a>

And my controller looks like this:

public function grupoPerfil($slug) {

        $grupo = Grupo::where('slug', $slug)->first();


            $rels = $grupo->relatos()->paginate(20);
            
            return view('perfilGrupo', compact(['grupo', 'rels']));

        
    }

I have 3 related models like this:

Grupo -> hasMany -> Relatos

Relato -> hasMany -> Comments

I've tried in many ways, but none work. It seems logical to me to do this:

 @foreach($rels->sortBy('created_at') as $r)
 @foreach($r->comments->paginate(10) as $c)
{...}

<div class="title"><a href="{{ route('relatoUser', [$r->grupo->slug, $r->slug] ) }}?page={{$c->lastpage()}}">{{$r->titulo}}</a>

{...}
@endforeach
@endforeach

But that doesn't work either. I have tried several similar alternatives but I cannot solve. Can anyone give me a light ?

0 likes
1 reply

Please or to participate in this conversation.