You don't :)
break, continue, return in blade template
How to use break, continue, return statements is a blade template
whatttttt?!
Well, you can do normal PHP inside it but depends on why you'd want to do that... maybe a foreach break?
Usually you wouldn't need to, but if you must, use <?php return; ?>
@bashy Yes. Isn't blade support for break statements? Or can we extend blade?
I don't think there's any native support for break, no.
You need to put it inside the php tag: and so on...
@intrip we've said about that already.
@foreach ($value_array as $value)
@if ($value == 'end')
@break;
@endif
{{ $value }}
@endforeach
That's because break doesnt exist. It's easy to extend.
why do we use blade again?
@t0ne Can use what you want.
Its simple to Use break inside the blade template. You can just put the php script inside the blade like , It will work and I used this method and got succeed.
@break doesn't seem to work in 5.1, just FYI
Anyone who needs to support it such as L5.1, see below:
Inside boot method of AppServiceProvider class in app/Providers, you can define a directive like this:
public function boot()
{
Blade::directive('break', function() { return "<?php break; ?>"; });
}
Don't forget to write use Blade; below the namespace declaration.
You can still use PHP in your Blade templates, and Blade @foreach is just implemented as a PHP foreach. So you can just do <?php break; ?>. You can also do <?php continue; ?> as well.
Whether or not that's a bad idea is up to you to decide. It's not pretty, and it would break if the way Blade implemented foreach in a different way. But if you're ok with it, then there's no reason you can't do it that way. But the Blade directive is prettier.
@foreach ($values as $value)
@if ($value < 0)
@php
continue;
@endphp
@endif
{{ $value }}
@endforeach
or
@foreach ($values as $value)
@if ($value < 0)
@continue
@endif
{{ $value }}
@endforeach
Both variants works in Laravel 5.6.
@Finesse You'd probably want to use the loop variable https://laravel.com/docs/5.6/blade#the-loop-variable
continue, can be achieved simply by adding an if condition to the contents of loop.
Hello, How can I skip the active post in the case of a blog?
Please or to participate in this conversation.