We help IT Professionals succeed at work.

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

Eduardo Fuerte
on
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.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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 FuerteDeveloper and Analyst

Author

Commented:
Hi Chris


Still with error.
 
img002
img003
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
OK.

Just had a quick look. We'll need to move some code around. The each() part of your Query Builder will need to be moved into the view. Change your Query Builder to the following:

$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);

Open in new window

And then in your view, when you loop through the $vitrinesX elements, add the liked_by property their instead. I can't remember how you're looping through in your view, but something like this:

@foreach ($vitrinesX as $vitrine)
@php
    $vitrine->liked_by= $vitrine->user_vitrine->pluck('id');
    $allowVote = ...
@endphp

Open in new window

The reason we need to do this is because the paginate() method works on a QueryBuilder, and by calling get() we execute the query and return the collection. We need to avoid doing that.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
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 ?
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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 FuerteDeveloper and Analyst

Author

Commented:
Hi Chris


It just showing 02 buttons...

img006
Sorry, adjusted....

img007
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Looks fine now...

It looks the buttons First and Last are not presented... is that right?
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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 FuerteDeveloper and Analyst

Author

Commented:
Chris


Thank you for this excelent solution!
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
You're welcome Eduardo, as always :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.