Laravel's new feature, high-order messaging

01-28-2024

The emergence of Laravel 5.4 framework will certainly. There are new functional features. One of them is the support for high-level messaging of collections. So-called Higher messaging (higher) Order Messaging) is actually a design pattern, which is used to query and manipulate object collections in a purely object-oriented way. The best way to show new features is through example code. Suppose you have a collection and want to operate on each element:

$invoices->each(function($invoice) { $invoice->pay(); });

With the new features, you can simplify the above code as follows:

$invoices->each->pay();

Another demonstration example is as follows:

$employees->reject(function($employee) { return $employee->retired; })->each(function($employee) { $employee->sendPayment(); });

Similarly, through the new features of 5.4, we can simplify it as follows:

$employees->reject->retired->each->sendPayment();

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us