Laravel Eloquent Where Query Examples
Today, i would like to show you laravel eloquent where condition. you will learn laravel eloquent where query. This tutorial will give you simple example of where condition in laravel query builder. This tutorial will give you simple example of where clause in query builder of laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.
I will give you simple examples of how to use sql where query in laravel. just see bellow simple examples that will help you how to write database where condition in laravel 7 application.
Syntax:
where('COLUMN_NAME', 'OPERATOR', 'VALUE')
SQL Query:
select * from `users` where `status` = 1
Example:
<?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("*")
->where("status", "=", 1)
->get();
dd($users);
}
}
Example 2: without Pass Operator
<?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("*")
->where("status", 1)
->get();
dd($users);
}
}
Example 3: Multiple Where Condition
<?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("*")
->where([
["status", "=", 1],
["email", "=", "yemmerich@example.net"]
])
->get();
dd($users);
}
}
Example 4: Some more
<?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("*")
->where("followers", ">", 100)
->get();
$users2 = User::select("*")
->where("followers", "<=", 1000)
->get();
$users3 = User::select("*")
->where("name", "like", "har%")
->get();
}
}
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 WhereIn Query Example
- How to use whereHas with orWhereHas in Laravel?
- Laravel Relationship Where Condition Example
- Laravel WhereIn() and WhereNotIn() with Subquery Example
- Laravel - whereDate(), whereMonth(), whereDay() and whereYear() Example
- Laravel Where Condition with Two Columns Example
- Laravel Where Clause with date_format() Example
- Laravel Eloquent Where Like Query Example Tutorial
- Laravel Where Clause with MySQL Function Example
- Laravel Query Builder Where Exists Example