ItSolutionStuff.com

Laravel Eloquent Where Not Equal To Condition Example

By Hardik Savani • November 5, 2023
Laravel

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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel withCount() with Where Condition Example

Read Now →

Laravel Where Clause with Function Query Example

Read Now →

Laravel Sum Query with Where Condition Example

Read Now →

Laravel Eloquent whereRelation() Condition Example

Read Now →

Laravel Eloquent whereHas() Condition Example

Read Now →

Laravel Relationship Where Condition Example

Read Now →

Laravel WhereIn() and WhereNotIn() with Subquery Example

Read Now →

Laravel - whereDate(), whereMonth(), whereDay() and whereYear() Example

Read Now →

Laravel Where Condition with Two Columns Example

Read Now →

Laravel Where Clause with date_format() Example

Read Now →

Laravel Eloquent Where Like Query Example Tutorial

Read Now →

Laravel Where Clause with MySQL Function Example

Read Now →

Laravel Query Builder Where Exists Example

Read Now →