Laravel Eloquent Where Not Equal To Condition Example
Hey Artisan,
This post will give you an example of laravel where condition not equal to. This example will help you laravel where not equal to condition. you will learn laravel eloquent where not equal to. I would like to share with you laravel eloquent where id not equal to.
In Laravel's Eloquent, you can use the where method to add conditions to your database queries, including conditions where a column is not equal to a specific value. You can achieve this by using the != operator or the <> operator. Here's an example of how to use both operators:
Assuming you have a users table and you want to retrieve all users whose status column is not equal to a specific value, let's say, not equal to "0". you can see the simple table with data:
users Table:
Let's see the both example one by one:
Laravel Where Not Equal To using "!=" Operator:
You can see the simple code of UserController File.
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$users = User::select("id", "name", "email", "status")
->where('status', '!=', '0')
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Admin User
[email] => admin@itsolutionstuff.com
[status] => 1
)
[1] => Array
(
[id] => 2
[name] => Manager User
[email] => manager@itsolutionstuff.com
[status] => 1
)
)
Laravel Where Not Equal To using "<>" Operator:
You can see the simple code of UserController File.
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$users = User::select("id", "name", "email", "status")
->where('status', '<>', '0')
->get();
dd($users);
}
}
Output:
Array
(
[0] => Array
(
[id] => 1
[name] => Admin User
[email] => admin@itsolutionstuff.com
[status] => 1
)
[1] => Array
(
[id] => 2
[name] => Manager User
[email] => manager@itsolutionstuff.com
[status] => 1
)
)
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 withCount() with Where Condition Example
- Laravel Where Clause with Function Query Example
- Laravel Sum Query with Where Condition Example
- Laravel Eloquent whereRelation() Condition Example
- Laravel Eloquent whereHas() Condition Example
- 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