Laravel Eloquent whereRaw Condition Example
Now, let's see post of laravel whereraw example. this example will help you laravel eloquent whereraw example. you will learn how to use whereraw in laravel. let’s discuss about whereraw concat in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.
Laravel provide new eloquent function whereRaw(), using whereRaw you can easily use mysql function and make it easily where condition. so here i will give you two example one concat function and another date_format function with whereRaw.
You can see bellow syntax on orWhere query in laravel:
whereRaw(SQL Condition, Array Value);
Example 1: Laravel whereRaw() with Concat()
SQL Query:
"select * from `users` where concat(first_name,' ',last_name) = ?"
Laravel Query:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$search = "Hardik Savani";
$users = User::select("*")
->whereRaw("concat(first_name,' ',last_name) = ?", [$search])
->get();
dd($users);
}
}
Example 2: Laravel whereRaw() with DATE_FORMAT()
SQL Query:
"select * from `users` where DATE_FORMAT(created_at, '%d-%m-%Y') = ?"
Laravel Query:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::select("*")
->whereRaw("DATE_FORMAT(created_at, '%d-%m-%Y') = ?", [date('d-m-Y')])
->get();
dd($users);
}
}
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
- Laravel Eloquent Where Query Examples
- Laravel Collection Merge | How to Merge Two Eloquent Collection?
- Laravel Multiple Where Condition Example
- Laravel Eloquent WhereNotIn Query Example
- Laravel Eloquent WhereIn Query Example
- How to use Union Query with Laravel Eloquent?
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Where Condition with Two Columns Example
- Laravel Where Clause with date_format() Example
- Laravel Query Builder Where Exists Example