Laravel Blade If Condition Example
Hi,
This simple article demonstrates of laravel blade if condition example. We will look at example of laravel blade if multiple conditions. i would like to share with you if condition in laravel blade. let’s discuss about laravel if condition in view.
i will give you simple example of how to write if else statement in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 example.
let's see one by one example bellow:
If Condition Example:
Syntax:
@if (condition)
/* Statements inside body of if */
@endif
Blade File Code:
@if($isAdmin == 1)
Admin User
@endif
Controller File Code:
<?php
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 Example:
Syntax:
@if (condition)
/* Statements inside body of if */
@else
/* Else body of if */
@endif
Blade File Code:
@if($status == 1)
Active
@else
InActive
@endif
Controller File Code:
<?php
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 Else If Condition Example:
Syntax:
@if (condition)
/* Statements inside body of if */
@elseif(condition)
/* if else inside body */
@else
/* Else body of if */
@endif
Blade File Code:
@if($status == 1)
Active
@elseif($status == 2)
Waiting
@else
InActive
@endif
Controller File Code:
<?php
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 it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Create Custom Blade Directive in Laravel?
- Laravel 8 Auth with Livewire Jetstream Tutorial
- Laravel Blade Check If Variable is Set or Not Example
- How to Write PHP Code in Laravel Blade?
- Laravel - How to Check If Array is Empty in Blade?
- How to Create Word Document File in Laravel?
- Laravel - How to Get .env Variable in Blade or Controller?
- How to Create Blade File in Laravel using CMD?
- How to use Inject View in Laravel?
- Laravel Ajax Render View With Data Example
- Laravel Blade Check if View Exists or Not Example