简体   繁体   中英

Laravel 5.7 Eloquent memory issues

I'm running the latest version of Laravel (v5.7.19), and have a little issue. I have a seeder than is running loop inside another one, and doing a simple query.

foreach ($tests as $test) {
    foreach ($anothers as $another) {
        Test::where('column', 123);
    }
}

This runs without any issues. I'm using memory_get_usage for the memory check.
As soon as I change the line to Test::where('column', 123)->first(); I run into huge memory problems. Does anyone have any ideas around this one?

Im assuming that this isnt your actual code.

Calling the functions ->first() or ->get() means you are fetching data from the database. Before that, Eloquent is just building your query, not actually loading any data.

I suspect you are pushing the result of your query into an array, possibly resulting in a huge array that is causing the memory issue. If your seeder is contained inside the loop and the objects are disposed of, i dont see why you would encounter memory issues.

Try unsetting all the variables you dont need inside the loop to not run out of memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM