HIGHER ORDER MESSAGES

Jan 05, 2024 Copy Link

If we have a chunk of users and we want to update their `email_verified_at`, we may write the next code:

 

use \App\Models\User;

User::get()->each(function ($user) {
    $user->update(['email_verified_at' => now()]);
});

 

But, Laravel comes with an amazing feature called Higher Order Messages which enables us to use some of its collection methods as if they are properties:

 

// \App\Http\Controllers\UserController.php

use \App\Models\User;

/**
 * Update the `email_verified_at` column of all registered users.
 */
public function updateEmailVerifiedAtOfAllUsers(): void
{
    User::get()->each->update(['email_verified_at' => now()]);
}

 

To deepen into this tip go ahead to Laravel docs.

Share via

Filed in:

Tips Collections
Mahmoud Ramadan

Mahmoud Ramadan

Mahmoud is the creator of Digging Code and a contributor to Laravel since 2020.

Most recent

  • How to generate Arabic PDF using TCPDF

    How to generate Arabic PDF using...

    FREE

  • What is SQL Injection

    What is SQL Injection

    FREE

  • Leveraging virtual generated columns in MySQL using Laravel

    Leveraging virtual generated col...

    FREE