Home » Laravel if statement multiple conditions – if condition in Laravel

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.

Read Also:  Laravel Eloquent Get Last Inserted ID using PHP

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

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

SELECT '(CASE 
            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("*",
                    \DB::raw('(CASE 
                        WHEN students.is_active = "0" THEN "Student" 
                        WHEN students.is_active = "1" THEN "Parent" 
                        ELSE "SuperParent" 
                        END) AS is_active_lable'))
                ->get();
   
dd($students);

Select with DB::raw() in Laravel

If Else In Db Raw Query In Laravel Example

$students = DB::table('students')
  ->select(DB::raw("
  name,
  surname,  
  (CASE WHEN (is_active = 1) THEN 'T' ELSE 'F' END) as is_active_text")
);
dd($students);

@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>         
@else
      <td>{{ $ensurance->status }}</td>        
@endif

@elseif Condition

if statement laravel

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

@isset Condition

@isset($tamilrokers)
    {{ $tamilrokers }}
@endisset

Laravel if isset() Example:

@if(isset($tamilrokers))
    {{ $tamilrokers }}
@endif

Blade if(isset) in Laravel

@isset($ensurance)
  // $ensurance is defined and is not null...
@endisset

@unless Condition

@unless checks in laravel if our expression statements returns FALSE

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

@unless instead of @if-statement

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

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

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

@endunless

@empty Condition

Laravel @empty Example:

Read Also:  Laravel 6 get ip address Example

@empty($tamilrokers)
    {{ $tamilrokers }}
@endempty

Laravel ternory Example:

{{ $tamilrokers ?? '' }}

I hope you get an idea about if condition in Laravel.
I would like to have feedback on my infinityknow.com 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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to write PHP code inside of Laravel Blade template?
  2. Read Also:  ajax live data search using jquery php mysql
  3. Read Also:  laravel checking if record exists
  4. Read Also:  Google Like Autosuggest Search Using PHP Ajax
  5. Read Also:  laravel excel import Export to Database Example
  6. Read Also:  Laravel Collection Contains with function
  7. Read Also:  PHP Laravel CKEditor Canned Responses using jquery
  8. Read Also:  jquery image upload preview Delete option
  9. Read Also:  Bash if else Statment
  10. Read Also:  Laravel 6.2 jQuery AJAX CRUD Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *