Sling Academy
Home/PHP/Laravel Eloquent: ‘EQUAL’ and ‘NOT EQUAL’ operators

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

Last updated: January 17, 2024

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.

Next Article: Eloquent: Sorting rows but empty values at the end/beginning

Previous Article: How to disable/enable timestamps in Laravel Eloquent

Series: Laravel & Eloquent Tutorials

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array