MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Conditional (@if, @else, @elseif, @isset, @empty, and @unless) Statements in Laravel.

First of all, we make a condition.blade.php file in which we define our conditions. See the below image.

Condition.blade.php view in the editor containing an h1 heading: This is condition page

After that, we make a function named condition in the controller and Defined a variable $name and assigned a string Sushant into it. After that returned it to view(condition.blade.php file is in a test folder). See the below image for help.

Laravel controller condition function setting $name to Sushant and returning the test.condition view

After that, we configure our Route. See below how I configured,

Laravel route Route::get condition to FirstController@condition, labeled URL, controller name and function name

@if and @else Condition

Condition wrote under if statement is executed when it’s True otherwise else executed. See the below Example:-

Sample Program

Blade @if and @else example checking whether strlen($name) is greater than 0 in condition.blade.php

In the above program, if-statement is true so it gives output String is > 0. See the output below:-

Output

Browser at 127.0.0.1:8000/condition showing This is condition page and output String is > 0

@elseif Condition

The else-if statement is written after if and before else statement and it executes when if statement is false. See the below example:-

Sample Program

Blade @elseif example checking whether strlen($name) is between 5 and 10 in condition.blade.php

In the above program, if condition is false so elseif condition checked whether it’s true or false, if it’s true then it executed otherwise else condition executed. But in the above program elseif condition is true, so it’s executed and gives the output:- String is in between 5 and 10. See the output below.

Output

Browser at 127.0.0.1:8000/condition showing @elseif output: String is in between 5 and 10

@isset Condition

@isset statement is used to check whether the variable we called is defined or not. If not defined then it’s not executed and prints next statement but doesn’t return an error. See the below example:-

Sample program

Blade @isset example checking an undefined $string variable, followed by Variable is not declared text

In the above program, Variable $string is not defined so @isset statement is not executed and the next statement prints. See the below output

Browser at 127.0.0.1:8000/condition showing @isset output: Variable is not declared

@empty Condition

The @empty statement is used to check whether the called variable has value or not. If no value is assigned to the variable then statement executed otherwise not executed. See the below example:-

Laravel controller condition() method setting $name to an empty string and passing it to the view

Sample Program

Condition.blade.php with @isset($name) and @empty($name) printing Value is empty

In the above program, Variable $name has no value, so when we check $name then it returns statement written under @empty condition.

Output

Browser output of the @empty example on the condition page showing Value is empty

@unless Condition

It is used to check whether the array or variable we called has any value declared or not. If no value is assigned to the variable or array then statement executed otherwise not executed. See the below example:-

Laravel controller condition() method setting $name to an empty string and passing it to the view

Sample Program

Condition.blade.php Blade code using @isset and @empty checks on $name for the @unless example

In the above program, there is no value assigned to the variable $name so it returns Value is empty. See the output below:-

Browser output at 127.0.0.1:8000/condition showing This is condition page and Value is empty

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

Laravel breeze vs laravel fortify comparison

Here’s a comparison table between Laravel Breeze and Laravel Fortify to help you understand their differences and choose the right tool for your project: Feature Laravel Breeze…

Read More

What is Laravel and use cases of Laravel?

What is Laravel? Laravel is an open-source, free PHP web framework designed for the development of web applications following the model-view-controller (MVC) architectural pattern. It was generated…

Read More

Laravel Tutorials: PHP Templating using Blade

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Complete PHP with Laravel Certification Guide & tutorials

What is PHP with Laravel? PHP was visioned eventually in the fall of 1994 by Rasmus Lerdorf. PHP means Hypertext Preprocessor. It’s a extensively- used, open source…

Read More

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Part-2

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Step 1. Create an index.blade.php file within resource/views/Country/ folder. Step 2.  Create a Controller within the…

Read More

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? Part-1

How to display a table in a Verticle or Horizontal form in the Laravel PHP Framework? How to Store Seed file into Database. Click There are two…

Read More