chunk (specified number);"; this method will update each user data and then Check the data of each user so that a large amount of data is not obtained when querying the data, which can reduce the pressure on the system."> How to use laravel's chunk method-Laravel-php.cn
Home> PHP Framework> Laravel> body text

How to use laravel's chunk method

WBOY
Release: 2022-06-06 15:12:55
Original
5312 people have browsed it

In laravel, the chunk() method is used to split a collection into multiple smaller collections of specified sizes. The syntax is "$specified collection->chunk(specified number);"; this method will Each user's data is updated before viewing each user's data. This way, a large amount of data is not obtained when querying data, which can reduce the pressure on the system.

How to use laravel's chunk method

#The operating environment of this article: Windows 10 system, Laravel version 6, Dell G3 computer.

How to use laravel's chunk method

chunk()

chunk method to split a collection into multiple smaller collections of a given size. Very useful for displaying collections into a grid.

$prices = collect([18, 23, 65, 36, 97, 43, 81]); $prices = $prices->chunk(3); $prices->toArray();
Copy after login

The above code generates the effect.

[ 0 => [ 0 => 18, 1 => 23, 2 => 65 ], 1 => [ 3 => 36, 4 => 97, 5 => 43 ], 2 => [ 6 => 81 ] ]
Copy after login

General use

If there is such a need: query all the data in the database and perform a series of updates

Simple and crude method All the queried data is operated using foreach and then stored in the database.

$users = User::all(); foreach ($users as $user) { $some_value = ($user->some_field > 0) ? 1 : 0; $user->update(['some_other_field' => $some_value]); }
Copy after login

If the data is very large, the system may run out of memory, and this method is obviously clumsy.

Laravel provides a simple solution for this

Laravel's chunk method can split the collection into multiple small collections of specified sizes

User::chunk(100, function ($users) { foreach ($users as $user) { $some_value = ($user->some_field > 0) ? 1 : 0; $user->update(['some_other_field' => $some_value]); } });
Copy after login

The principle of running the above code Yes:

Run a chunk, this chunk finds 100 user data, and updates each user data

Then check 100 user data, then perform the update operation, and so on.

This means that when querying data, you will not get a large amount of data from the database. Instead of querying the entire table, you will query 100 pieces of data each time.

This will reduce the pressure on the system.

2. Things to note

The focus of this article is:

But be careful, you cannot run it like this if you have filtered results

User::where('approved', 0)->chunk(100, function ($users) { foreach ($users as $user) { $user->update(['approved' => 1]); } });
Copy after login

From the code Executionally speaking, this statement does not cause any errors

But the problem here is that you want to filter users with approved=0, perform database operations with approved=1

and then chunk the next one 100 pieces of data, the data has changed at this time and you will miss one page of data.

This means you will only process half of the entries and leave half out.

Solution:

You can use the chunkById method after Laravel 5.2

You can refer to the following code before Laravel 5.2

while(User::where('approved', 0)->count() > 0) { User::where('approved', 0)->chunk(100, function ($users) { foreach ($users as $user) { $user->update(['approved' => 1]); } }); }
Copy after login

[Related recommendations:laravel video tutorial

The above is the detailed content of How to use laravel's chunk method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!