Baldráni
Baldráni

Reputation: 5640

How to limit a foreach on an object in Laravel?

So I'm working with Laravel and I'm trying to return the whole part of an object except the first thing. I tried array_slice() as shown above but it break the page.

{{gettype($formulas)}} --> object

@foreach(array_slice($formulas->toArray(), 1, 5) as $f)
    <label>{{$f->name}}</label>
@endforeach

So is there a way to do the same thing but with an object?

Upvotes: 2

Views: 1770

Answers (2)

Bhavin
Bhavin

Reputation: 2168

May be this will work i didn't tried it but may be it will work.

<?php $count = 0; ?>
@foreach ($formulas->toArray() as $f)
    <?php if( !$count == 0){ continue; } ?>
    // Your code here           
    <?php $count++; ?>
@endforeach

Upvotes: 1

This could help you :

$limit = 5;
@foreach( $formulas as $index => $f)
    @if( $index >= $limit )
        @break
    @endif
    @if ( ! $index == 0 )
        <label>{{$f->name}}</label>
    @endif
@endforeach

Upvotes: 1

Related Questions