You can ask the $loop variable if it's odd or not
@if ($loop->odd)
@endif
@if ($loop->even)
@endif
Documentation: https://laravel.com/docs/5.8/blade#the-loop-variable
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?
You can ask the $loop variable if it's odd or not
@if ($loop->odd)
@endif
@if ($loop->even)
@endif
Documentation: https://laravel.com/docs/5.8/blade#the-loop-variable
@BOBBYBOUWMANN - Thank you, Bobby. It results in Undefined property: stdClass::$odd
Will it check if odd on every pagination link?
What version of Laravel do you use? This was added in a later version!
If you use another version you might want to checkout the docs for that version! Just replace your version number in the following url: https://laravel.com/docs/5.8/blade#the-loop-variable
@BOBBYBOUWMANN - 5.8.4, should work, normally.
I'm not sure what you mean with every pagination link? Can you show some code there?
@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
Okay, so I understood correctly :)
But your example in the first post should work for that. What's wrong with it?
@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.
Oooh...
// if odd
$users->count() % 2 != 0
There you go :) You can add that after your foreach loop
@FTIERSCH - Magical! Thank you.
How is $users->count() % 2 != 0 different from using odd and even?
@BOBBYBOUWMANN - I don't know why it threw me the error. Even though it should work, within 5.8.4.
@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
Here you go:

@BOBBYBOUWMANN - Ah crap, google showed me a third party page :D Thanks!
Still not the right use case here
Aah yeah your correct! I was mixing up things! :see_no_evil:
@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?
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.
@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.
@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).
What is the current output, this thread becomes a little bit unclear at this point!
@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.