Laravel Eloquent Where Not Equal To Condition Example

By Hardik Savani November 5, 2023 Category : 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...

Shares