In this article, I will share with you how to manage If elseif and else condition in laravel blade file. as you all know laravel is a provide manu easy to use functionality to use more make application code more less. in this example we see how to manage laravel blade file HTML content base on if, elseif or else condition base.
If Condition Example
Syntax
@if (condition)
/* Statements inside body of if */
@endif
Blade File Example
@if($isAdmin == 1)
Admin User
@endif
Controller File Example
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function index()
{
$isAdmin = 1;
return view('ajaxRequest', compact('isAdmin'));
}
}
If - else Condition
Syntax
@if (condition)
/* Statements inside body of if */
@else
/* Else body of if */
@endif
Blade File Example
@if($status == 1)
Active
@else
InActive
@endif
Controrller File Example
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function index()
{
$status = 1;
return view('ajaxRequest', compact('status'));
}
}
If - elseif -else Condition
Syntax
@if (condition)
/* Statements inside body of if */
@elseif(condition)
/* if else inside body */
@else
/* Else body of if */
@endif
Blade File Example
@if($status == 1)
Active
@elseif($status == 2)
Waiting
@else
InActive
@endif
Controller File Example
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AjaxController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function index()
{
$status = 3;
return view('ajaxRequest', compact('status'));
}
}
i hope you like this article.