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
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')On/Off @else{{ $ensurance->status }} @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:
@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.