php - Too many rows causing 500 error?


I have 40k entries in a database, and I am trying to call them using a simple fetch in laravel.

$domains = Domain::where("available", 1)->limit(1000)->get();
return view('domains')
    ->with("domains", $domains);

This works fine up to a few thousand rows. But if I set no limit on the call, I receive a 500 error. I can't fathom why, and I can't work out where I would look to discover how to avoid this issue, I can't seem to find anything in the apache logs, or laravel's own logs located in storage.




You can avoid this issue by leveraging the->chunk command.

Domain::where('available', 1)->chunk(200, function($domain){
    //do whatever you would normally be doing with the rows you receive
    // $domain stuff

The purpose of thechunk command is to free up memory after everyX iterations of the Model. In this case, I've shown 200.

Sidenote - As the chunk method uses aClosure as the 2nd argument, ensure youuse($your_varaibles);

People are also looking for solutions to the problem: Create a Dynamic insert statement - PHP - Mysql


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.