kendrick's avatar

How to check if $collection is odd, within the current pagination?

I am trying to paginate(2) users within my view. And check on every pagination "side", if the number is odd, so I can display a blank field for the odd case.

So if I count 3 users, on the first pagination I would show two, and on the second 1 user and 1 space filler (because odd).

Odd logic would be

@foreach($users as $user) 
    @if(!$loop->iteration  % 2 == 0)
         
    @endif
@endforeach

But, how can I check on the current pagination, or this even possible?

0 likes
22 replies
kendrick's avatar

@BOBBYBOUWMANN - Thank you, Bobby. It results in Undefined property: stdClass::$odd

Will it check if odd on every pagination link?

ftiersch's avatar

I'm not sure what you mean with every pagination link? Can you show some code there?

kendrick's avatar

@FTIERSCH - As I paginate(2) users, I mean with links, that If we count 3 users, we get two pagination links, 1 (showing the first 2), and 2(showing the third user). And at 2 I want to implement a space filler for the blank "fourth" user.

That is why I wanted to check on every pagination link, if odd or not.

@foreach($users as $user) 

 @if($loop-even)
  <div>{{$user->name}}</div>
@else
 <div>space-filler</div>
@endif

@endforeach
ftiersch's avatar

Okay, so I understood correctly :)

But your example in the first post should work for that. What's wrong with it?

kendrick's avatar

@FTIERSCH - It works, but checks on the whole collection, not the current pagination logic. So if the collection counts 3 users, it implements the space-filler under every user. And not only the second pagination side, where the third, odd user should be completed with a space filler.

ftiersch's avatar
ftiersch
Best Answer
Level 28

Oooh...

// if odd
$users->count() % 2 != 0

There you go :) You can add that after your foreach loop

1 like
bobbybouwmann's avatar

How is $users->count() % 2 != 0 different from using odd and even?

ftiersch's avatar

@BOBBYBOUWMANN - I didn't find anything about odd / even in the docs but also he needs it depending on the number of items in the collection, not the current "state" of the $loop variable

bobbybouwmann's avatar

Aah yeah your correct! I was mixing up things! :see_no_evil:

1 like
kendrick's avatar

@FTIERSCH - This logic is super for a vertical foreach loop. If we imagine a horizontal loop, where we implement a row with three elements per row, and max. our collection to lets say six elements, how could we fill the spaces per row, with the same logic in mind as within the vertical loop.

Is this even possible to a certain extend?

bobbybouwmann's avatar

You can use chunk to get a collection per row you want. But you still need some custom logic to determine if you're going to show two or one space-fillers.

kendrick's avatar

@BOBBYBOUWMANN - Could you give me a quick chunk example? Never used it.

Currently the space filler logic looks like this:

I limit the collection within my Controller to e.g. 6.

And within my view, I implement this logic, and for every case either show one space filler or two (simple partials):

@if($users->count() == 1) 
    @elseif($users->count() == 2) 
    @elseif($users->count() == 3) 
    @elseif($users->count() == 4) 
    @elseif($users->count() == 5) 
    @elseif($users->count() == 6)
@endif 

Would love to put some more logic in it.

kendrick's avatar

@BOBBYBOUWMANN - Yes, within the logic above, I am not paginating, only limit(6)->get().

It works, but was searching for a more logic or dynamic solution. Because then I always need to apply the redundancy of checking how much the collection currently counts (within the view).

bobbybouwmann's avatar

What is the current output, this thread becomes a little bit unclear at this point!

1 like
kendrick's avatar

@BOBBYBOUWMANN - Sorry for that.

That is my current horizontal loop logic. And I wondered about a more efficient and dynamic solution for that logic, a less redundant one.


@if($users->count() == 1) 

<div class="row">
@foreach($users as $user)
<div class="col-md-4"> 
<p>{{$user->name}}</p> 
</div> 
@endforeach
<div class="col-md-4"><p>space-filler</p></div>
<div class="col-md-4"><p>space-filler</p></div>
</div>

@elseif($users->count() == 2) 
    
<div class="row">
@foreach($users as $user)
<div class="col-md-4"> 
<p>{{$user->name}}</p> 
</div> 
@endforeach
<div class="col-md-4"><p>space-filler</p></div>
</div>

@elseif($users->count() == 3) 

<div class="row">

@foreach($users as $user)
<div class="col-md-4"> 
<p>{{$user->name}}</p> 
</div> 
@endforeach
</div>

@elseif($users->count() == 4) 
... same as 1
@elseif($users->count() == 5) 
... same as 2
@elseif($users->count() == 6)
... same as 3
@endif 

Please or to participate in this conversation.