Laravel Eloquent: ‘EQUAL’ and ‘NOT EQUAL’ operators

Updated: January 17, 2024 By: Guest Contributor Post a comment

Introduction

When it comes to web development with PHP, Laravel is one of the most popular and clean frameworks available today. Its Eloquent ORM provides an elegant and powerful ActiveRecord implementation to work with your database. In this tutorial, we’ll dive into the usage of ‘EQUAL’ and ‘NOT EQUAL’ operators in Eloquent queries, enhancing your database querying skills within Laravel.

Eloquent is an ORM (Object-Relational Mapper) in Laravel which makes it incredibly easy to interact with a database. When using Eloquent, each database table has a corresponding “Model” which is used to interact with that table. The ‘EQUAL’ and ‘NOT EQUAL’ operators are among the most basic and most used SQL operations, allowing you to filter results based on exact matches or mismatches.

Basic Usage of EQUAL Operator

In its simplest form, the EQUAL operator in Eloquent can be used like this:

<?php

$users = App\Models\User::where('email', '[email protected]')->get();

foreach ($users as $user) {
    echo $user->name;
}

?>

This code will retrieve all users whose email is exactly ‘[email protected]’.

Using EQUAL with Multiple Conditions

Sometimes, you need to apply multiple EQUAL conditions at once:

<?php

$users = App\Models\User::where('status', 'active')
    ->where('role', 'user')
    ->get();

?>

This will fetch all users who are active and have the ‘user’ role.

Basic Usage of NOT EQUAL Operator

The NOT EQUAL operator in Eloquent is used to exclude records with a certain value:

<?php

$users = App\Models\User::where('email', '!=', '[email protected]')->get();

?>

This will retrieve all users except for ‘[email protected]’.

Advanced Queries with Conditional Clauses

For more complex scenarios, conditional clauses can use ‘EQUAL’ and ‘NOT EQUAL’ logically:

<?php

$users = App\Models\User::where(function ($query) {
    $query->where('last_login', '>', '2021-01-01')
          ->orWhere('role', '!=', 'guest');
})->get();

?>

This Eloquent query looks for users who have logged in after the start of 2021 or are not guests.

JOIN Operations and EQUAL Operator

We can also join tables and use the EQUAL operator to obtain related data:

<?php

$posts = App\Models\Post::join('users', 'users.id', '=', 'posts.user_id')
    ->where('users.name', 'Jane Doe')
    ->select('posts.*')
    ->get();

?>

This fetches all the posts written by ‘Jane Doe’.

Using Raw Expressions

If you need to use complex SQL with Eloquent’s ‘EQUAL’ or ‘NOT EQUAL’, you can use raw expressions:

<?php 
$users = App\Models\User::whereRaw('length(name) = ?', [5])->get(); 
?>

This gets all users with a name of exactly 5 characters long.

Conclusion

Using ‘EQUAL’ and ‘NOT EQUAL’ operators correctly can fine-tune the queries you run on your database. With this knowledge, you can now manipulate data sets with precision, using Eloquent’s fluent and expressive syntax to your advantage.