Laravel,是否有可能打破foreach内部的陈述?

I need to break if statement inside foreach once the the if condition is true. Is it possible to break the if statement without breaking the foreach?

Below shows the codes that I was currently using now. But if I do this, it break the foreach too.

Codes

@foreach($furniture as $fur)
{
  //do something
  @if($fur->broken == true)
    //do something
    <php break; ?>
  @endif
}
@endforeach

I would use a do while inside the foreach

foreach($furniture as $fur){
//do something
    do{
        if($fur->broken == true){
          //do something
           break;
        }
    }while(false);
}

The Do While evaluates the condition after the first iteration, in this case the loop ends after the first iteration (because the condition is false), but this gives us a structure we can break out of.

The other way to do this is to use a switch statement with an if in it.

foreach($furniture as $fur){
//do something
    switch(1){
        default:
           if($fur->broken == true){
            //do something
             break;
           }
    }
}

Also note to break out of the foreach you can do break 2 instead of just break.

All that said, I would say just from a best practice standpoint this makes the code less readable and what you need could be done without it ( most likely ) but I would need more details to help with that.

UPDATE

IS this a template thing like ( Blade )? It's not clear in the question. I don't use blade, but I've seen it, studied it some, so that makes sense with the @ sign. But as I took the time to type this I'll leave it here.

Of note the same could probably be done in blade by faking a loop ( loop 1 time ) inside the foreach, same principle as the do while. Assuming it supports break levels. Something like this

 @foreach($furniture as $fur)
    //do something 
     @foreach($fake as $f)     
         @if($fur->broken == true)           
          //do something
             @break
         @endif
     @endforeach
  //do something else
@endforeach
 //where fake is like  $fake = [1];  single element, so one loop.

You could also do the extra loop inside or outside the if block, as your needs dictate.

I am also not sure if Blade supports switch most template engines don't, but it may be cleaner to use if it does.

Check the Laravel documentation: https://laravel.com/docs/5.5/blade

Example from documentation:

@foreach ($users as $user)
    @if ($user->type == 1)
        @continue
    @endif

    <li>{{ $user->name }}</li>

    @if ($user->number == 5)
        @break
    @endif
@endforeach

use continue instead of break:

@foreach($furniture as $fur)
  //do something
  @if($fur->broken == true)
    //do something
    @continue
  @endif

@endforeach

if is not a loop structure, so you cannot "break out of it".

You can, however, break out of the foreach by simply calling break. In your example it has the desired effect:

foreach($furniture as $fur) {
//do something
if ($fur->broken == true) {
    //do something            


    // will leave the foreach loop and also the if statement
    break;
}
this_command_is_not_executed_after_a_match_is_found();

}