Sling Academy
Home/PHP/How to register and use closures in Eloquent

How to register and use closures in Eloquent

Last updated: January 16, 2024

Introduction

Eloquent is a powerful ORM (Object-Relational Mapping) tool that comes with Laravel, offering a beautiful, simple ActiveRecord implementation for working with your database. One of the less frequently discussed but incredibly useful features of Eloquent is the ability to use closures, or anonymous functions, to encapsulate query logic. This tutorial will guide you through the process of registering and using closures in Eloquent, helping you write clean and maintainable code.

Understanding Closures in PHP

Before diving into Eloquent, it’s essential to understand what a closure in PHP is. A closure is an anonymous function that can inherit variables from the parent scope. It is often used for functions that require no names, like callbacks or event listeners.


$greet = function($name)
{
    return "Hello " . $name;
};

echo $greet("World"); 
// Outputs: Hello World

Basic Use of Closures in Eloquent

In Eloquent, closures can be passed as arguments to methods like where, when, and with, allowing you to build complex queries in a readable and reusable manner. For example:


$users = User::where(function ($query)
{
    $query->where('age', '>', 30)
          ->where('votes', '>', 100);
})->get();

This will fetch all users who are older than 30 and have more than 100 votes. The closure groups the conditions within the where method, making it easy to understand what the query does at first glance.

Advanced Closure Usage

As you become more comfortable with closures in Eloquent, you can start to use them for more complex scenarios. For example, you can use closures to define relationship constraints:


$users = User::with(['posts' => function ($query)
{
    $query->where('title', 'LIKE', '%first%');
}])->get();

This will load all users along with their posts where the title contains the word ‘first’. Closures allow for conditionally loading relationships without loading the entire relationship data.

Using Closures for Conditional Clauses

Another powerful way to use closures is for conditional clauses. The when method in Eloquent accepts a closure and a boolean value. When the boolean value is true, the closure will execute:


$search = 'John Doe';
$users = User::when($search, function ($query, $search)
{
    return $query->where('name', $search);
})->get();

If $search is a non-empty string, the search condition will be applied, otherwise, no search will be performed.

Beyond Basic Queries

By combining closures with Eloquent’s query methods, you can create reusable scope methods. Scopes can be defined in your Eloquent model and can take closures to further customize the query:


public function scopeOfType($query, $type)
{
    return $query->where('type', $type);
}

$admins = User::ofType('admin')->get();

Scopes offer a straightforward way to encapsulate query logic and can significantly clean up your controller code.

Dynamic Relationships Using Closures

Eloquent allows the use of closures to create dynamic relationships. This is particularly useful when the relationship is not predefined in the model:


$account = Account::find(1);

$account->setRelation('dynamicUsers', $account->belongsToMany(User::class)->wherePivot('attribute', 'value')->get());

Conclusion

This tutorial explored how closures not only lead to cleaner code but also empower Eloquent with enhanced flexibility and maintainability. By mastering the use of closures in your Eloquent queries, you can write more expressive, concise, and reusable database interactions.

Next Article: How to define and use observers in Eloquent

Previous Article: How to Compare Two Models in 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