PHP Fatal error: bytes exhausted After running query

Asked

Viewed 80 times

0

Hello, in a certain process of my code I need to execute a query that returns me 4500 rows from the database

 return Property::whereHas('portals', function ($query) {
        $query->where('portal_id', '=', '1');
    })->get();

After running this query I do a check on each line.

The problem is that before checking php accuses a memory error PHP Fatal error: Allowed memory size of 16777216 bytes exhausted

In local environment I changed the memory_limit in php.ini to 512M and the function ran OK. The problem is that this is not a solution for me, that I will run it on online server.

I need to split this result before I run the check, but I don’t know how, someone can help me?

  • 2

    Did you limit the query? Making a query without limit is something unfeasible, it would be better to implement pagination.

  • How do I implement paging? I can’t put a limit on the query because I can have an infinite number of Properties

  • Ivan I’m tetando here, but I think you’ll have to use ::has, however I will read the documentation and talk to a colleague, because I really do not understand of Eloquent (is that I use another framework)

1 answer

0


Depends on the version of your Laravel, in older versions you can use:

 return Property::whereHas('portals', function ($query) {
    $query->where('portal_id', '=', '1');
})->take(10)->skip(10)->get();

Who would take (TAKE) 10 results after skipping (SKIP) the top 10;

In more recent versions the code would be:

return Property::whereHas('portals', function ($query) {
    $query->where('portal_id', '=', '1');
})->limit(10)->offset(10)->get();

Then to page your result you would only change the OFFSET or SKIP.

  • my function looked like this: public Function get($Skip, $limit) { $propertyList = Property::whereHas('Portals', Function ($query) { $query->Where('portal_id', '=', '1'); })->limit($limit)->Skip($Skip)->get(); } the problem, is how to know how many I have to give Skip every time I run this function

  • Make a counter, with a variable any $skipped = 0; every time you call this your function you make $skipped += $Skip, where $Skip was the value "skipped". Remember that your get($Skip, $limit) function must receive $skipped instead of $Skip.

  • The counter in my case didn’t help was for($skiped = 0 ; $skiped < = Count($properties) ; $skiped += Count($p)){ $this->get($skiped, Count($properties)); }

  • I made an example to illustrate my reasoning: http://paste.ofcode.org/35jTyH6csvu5Zhx3j3kvrPa

  • I did not understand how I will apply this reasoning in my case since I have to do several checks and send to a queue.http://paste.ofcode.org/ReZsTq3PxS8bKXqNSGnApk

  • Look if it facilitates: http://paste.ofcode.org/YAHx5QK7UdcL7KDgTfPZ5m

  • So http://paste.ofcode.org/3b3spfNSkLeGsQ3qW7pD8Cf is right?

  • With that part of the code that Voce is passing through I can not know what is missing, but how it does not work, I commented: http://paste.ofcode.org/xe2pqGKR6xPSdNdFeTQNtk

  • You’re right, I tested it here and it didn’t work, I made some changes http://paste.ofcode.org/JUjpj2Aue5EfvATibPdTfV

  • I believe it is clear: http://paste.ofcode.org/3afx8PxdQGHck3xhh2kxpV

  • 1

    Now I get it, thanks!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.