Skip to content
Advertisement

Laravel – simpliest elseif [closed]

@foreach ($todos as $todo)
@if($todo->is_completed == 1)
    <tr class="bg-success">
@elseif($todo->level == 1)
    <tr class="bg-primary">
@elseif($todo->level == 2)
    <tr class="bg-warning">
@elseif($todo->level == 3)
    <tr class="bg-danger">
@endif

Hi, have i to do it more simple or its a normal practice?

Advertisement

Answer

Instead of using if-else, I would recommend you to use switch statement.

@foreach ($todos as $todo)
   @if($todo->is_completed == 1)
     <tr class="bg-success">
   @endif

   $level= $todo->level;

  @switch($level)
       @case('1')
           <tr class="bg-primary">
       @break
 
       @case('2')
            <tr class="bg-warning">
       @break
 
       @case('3')
            <tr class="bg-danger">
       @break
  @endswitch
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement