mvnobrega
3 months ago
381
3
General

Link on another page to last pagination URL [ SOLVED ]

Posted 3 months ago by mvnobrega

I am working on a project similar to a discussion forum. I need to create a link that takes the user to the last URL paginate.

My group controller :

public function gruposIndex (Request $ request) {

     $ groups = Group :: paginate (9);

         return view ('dreams', compact ('groups'));
     }

My groups view:

@foreach($grupos as $g)     
                           
                    <div class="testimonial">
                        <div class="meta">

                            <div class="avatar has-photo"><img src="{{Storage::url($g->foto)}}"></div>
                    <div class="meta-perfil-relatos">
                        <div><span class="fas fa-calendar-alt"></span>0</div>
                        <div><span class="fas fa-thumbs-up"></span>0</div>
                        <div><span class="fas fa-comments"></span>85</div>
                    </div>
                           
                        </div>
                         <div class="author">
                                <h2>{{$g->titulo}}</h2>
                            </div>

                        <div class="btn-sonhos">
                            <a href="{{ route('grupoPerfil', $g->slug)}}" class="btn btn-action">Entrar<span class="far fa-arrow-right"></span></a>
                        </div>
                        
                    </div>
                @endforeach  

When the user clicks the link: {{ route('grupoPerfil', $g->slug)}} I need it to be taken to the last pagination link, for example www.site.com/category/dreams?page=22

Which in the controller looks like this:

public function grupoPerfil($slug) {


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

        $rels = $grupo->relatos()->paginate(10);

        if($grupo){
            return view('perfilGrupo', compact(['grupo', 'rels']));
        }

        abort(404);    
        
    }

And view:

 @foreach($rels->sortBy('created_at') as $r)
            <div class="thread ">
                <div class="main-info">
                    <div class="avatar"><img src="{{$r->user->avatar}}" title="Posted by Stephen"></div>
                    <div class="name">
                        <div class="title"><a href="{{ route('relatoUser', [$r->grupo->slug, $r->slug] ) }}">{{$r->titulo}}</a></div>
                        <div class="excerpt">
                            {{str_limit($r->relato, 60)}}
                        </div>
                    </div>
                </div>
                <div class="meta">
                    <div class="answer-count"><span class="far fa-comment"></span> <span class="simple-text">{{$r->comments->count()}}</span> 
                        <span class="nice-text">{{$r->comments->count()}} respostas</span>
                    </div>
                    <div class="date">
                        <div class="poster">
                            {{$r->user->username}}
                        </div>
                        <div class="diff">em {{$r->created_at->format('d/m/Y')}}</div>
                    </div>
                </div>
            </div>
            @endforeach
            
            {{ $rels->links() }}

Need this solution because the last page of {{ $rels->links() }} will be the most recent

I am having trouble finding a solution to this. Can someone help me?

Please sign in or create an account to participate in this conversation.