Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag for Brazil asked on

Could you point how to correctly use Laravel's paginate method at this code?

Hi Experts

Could you point how to correctly use Laravel's paginate method at this code?

    $vitrinesX = Vitrine
           ::with('user_vitrine')
	   ->ativa()
	->aprovada()
	->global(Auth::user()->tipoparticipante_id)
	->orderBy('id','desc')
	->withCount([
		'user_vitrine as qtdeCurtiu' => function (Builder $query) { $query->where('curtiu', 1); },
		//'user_vitrine as naocurtiu' => function (Builder $query) { $query->where('curtiu', 0); }
	])
	->get()->paginate(5)
	->each(function($vitrine) {
		$vitrine->liked_by= $vitrine->user_vitrine->pluck('id');
	});

Open in new window


Produces this error:

img001
Thanks in advance.
LaravelPHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Chris Stanyon

Hey Eduardo,

I can't test at the moment, but I think you need to re-order your methods slightly:

$vitrinesX = Vitrine
    ::with('user_vitrine')
        ->ativa()
        ->aprovada()
        ->global(Auth::user()->tipoparticipante_id)
        ->orderBy('id','desc')
        ->withCount([
            'user_vitrine as qtdeCurtiu' => function (Builder $query) { $query->where('curtiu', 1); },
            //'user_vitrine as naocurtiu' => function (Builder $query) { $query->where('curtiu', 0); }
        ])
        ->paginate(5)
        ->get()
        ->each(function($vitrine) {
            $vitrine->liked_by= $vitrine->user_vitrine->pluck('id');
	});

Open in new window

The paginate() method need to go before the get() method.
Eduardo Fuerte

ASKER
Hi Chris


Still with error.
 
img002
img003
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Eduardo Fuerte

ASKER
So I adapted the existent code to:


    <section class="row">
           
                    @foreach($vitrinesX->chunk(3) as $row)
                    
                        @foreach($row as $vitrine)
      
                      
                           <div class="col-sm-12 col-md-4">
                           
                                @php 
                                
                                    // Added ------------------------------------------------
                                    $vitrine->liked_by= $vitrine->user_vitrine->pluck('id');
                                    //--------------------------------------------------------
                                    
                                    $currentUserId = auth()->user()->id;
                                    $hasUserVoted = $vitrine->liked_by->contains($currentUserId); 
                                @endphp
                           
                                <div class="box-noticias">
                                   	<a href="javascript:void(0);" onclick='hotsite.vitrine.abrirVitrine({{$vitrine->id}});' >
                                    <img src="{{ $vitrine->url }}">
            	    			    </a>                    
                                </div> 
                                
                                <p id="like" class="likes" @if (!$hasUserVoted ) onclick="hotsite.vitrine.salvarEscolha({{ $vitrine->id }}, 1, this)" @endif>
                                    <i class="fa fa-thumbs-up"></i> <span id="qtdeCurtiu_{{ $vitrine->id }}">{{ $vitrine->qtdeCurtiu }}</span>
                                </p> 
      
                 
                           </div>     
                          
                        @endforeach
                        
                    @endforeach
                      
                                              
            </section>

Open in new window


And that runs!!!

Just another issue.
The First, Previous, Next, Last  bar isn't presented... could  you point what's also needed ?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Chris Stanyon

Hi Eduardo,

To show the pagination bar, you need to echo out the links() method:

<section class="row">
    ...
</section>
{{ $vitrinesX->links() }}

Open in new window

Eduardo Fuerte

ASKER
Hi Chris


It just showing 02 buttons...

img006
Sorry, adjusted....

img007
Eduardo Fuerte

ASKER
Looks fine now...

It looks the buttons First and Last are not presented... is that right?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Stanyon

Excellent,

No - by default, you won't get a First Page and Last Page, but if you really want them, you can customise the View used to display the pagination. Have a read through the docs - https://laravel.com/docs/6.x/pagination#customizing-the-pagination-view- to see how to do it. Basically, you can either provide your own view for the Page Links, or you can publish and edit the Default view. You'd then just add a link for the First and Last page accordingly.
Eduardo Fuerte

ASKER
Chris


Thank you for this excelent solution!
Chris Stanyon

You're welcome Eduardo, as always :)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23