Laravel blade Switch case for multiple Condition

Laravel blade switch multiple case example; In this tutorial, you will learn how to use a switch case for multiple conditions in laravel blade.

We already know switch statements can be constructed using the @switch, @case, @break, @default, and @endswitch directives. Using the below example you can understand the concept of laravel switch case example.

Adding switch case statement in laravel 6, laravel 7, laravel 8, laravel 9 blade view file.

Syntax:

@switch($i)
    @case(1)
        First case...
        @break

    @case(2)
        Second case...
        @break

    @default
        Default case...

@endswitch

Blade File Code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
  
@switch($status)
    @case(1)
        Active
        @break
  
    @case(2)
        Wait
        @break
  
    @default
        InActive
@endswitch
  
</body>
</html>

Controller Code:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function index()
    {
        $status = 1;
  
        return view('users', compact('status'));
    }
}

Hope this example helps you…

Leave a Comment