Laravel if statement multiple conditions – if condition in Laravel

I will learn how to use if condition in laravel select Db Raw Query with Conditional (@if, @else, @elseif, @isset, @empty, and @unless) Statements in Laravel. i can use mysql case when statement with db raw in laravel. If you want to use mysql method then you must have to use db raw method. in this Laravel example i will display you laravel select Db Raw Query with Laravel Sql Case When if condition.

In minor case I need to use if else condition(laravel db::raw if) in laravel DB Eloquent. It might be use If Else In Db Raw Query In Laravel Multiple Conditions – IF ELSE when you just need to send data to import or admin server side api etc.

I will give you Laravel Select Query if Condition when you can use this kind of when case statement in mysql query. If you take “is_active” fields in students table with following meaning:
Laravel If, Else and Elseif Conditional Statements

  • 0 => Student
  • 1 => Parent
  • 2 => SuperParent
Also Read This πŸ‘‰   php array length - Best 3 Ways To Find Array Length in PHP

How to use if condition in laravel select query?

So at this time i save the collumn “is_active” 0 1 or 2 each values in database Table table but when i select at that time it should become “Student” “Parent” and “SuperParent”. Also you can check my prev Article for If Condition In Laravel Controller

Use The mysql Query Builder

            WHEN students.is_active = "0" THEN "Student" 
            WHEN students.is_active = "1" THEN "Parent"
            ELSE "SuperParent" 
            END) AS is_active_lable'
FROM students

Use The Laravel Query Builder

$students = Student::select("*",
                        WHEN students.is_active = "0" THEN "Student" 
                        WHEN students.is_active = "1" THEN "Parent" 
                        ELSE "SuperParent" 
                        END) AS is_active_lable'))

Select with DB::raw() in Laravel

If Else In Db Raw Query In Laravel Example

$students = DB::table('students')
  (CASE WHEN (is_active = 1) THEN 'T' ELSE 'F' END) as is_active_text")

@if and @else Condition

@if($ensurance->status =='waiting')         
      <td><a href="#" role="button" data-id="{{ $ensurance->ensurance_id }}" data-toggle="modal" data-target="#myModal">On/Off<a></td>         
      <td>{{ $ensurance->status }}</td>        

@elseif Condition

if statement laravel

@if (ensurance)
  // ensurance do something
@elseif (bar)
  // ensurance do something else
  // ensurance do some other thing;

@isset Condition

    {{ $tamilrokers }}

Laravel if isset() Example:

    {{ $tamilrokers }}

Blade if(isset) in Laravel

  // $ensurance is defined and is not null...

@unless Condition

@unless checks in laravel if our expression statements returns FALSE

@unless (Auth::check())
    You are not signed in.

@unless instead of @if-statement

@if (!Auth::check())
    Please log in.

@unless (Auth::check())
    You are not ensurance signed in.

@unless(count($posts) == 0)
{{ $post->title }}


@empty Condition

Laravel @empty Example:

Also Read This πŸ‘‰   how to check email already exist in database in php? - 5 Ways To Check if Email already exists Using PHP

    {{ $tamilrokers }}

Laravel ternory Example:

{{ $tamilrokers ?? '' }}

I hope you get an idea about if condition in Laravel.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.