PHP Fatal error: bytes exhausted After performing query laravel

0
Hello, in a given 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 executing this query I check each line.

The problem is that before doing the check the php has a memory error PHP Fatal error: Allowed memory size of 16777216 bytes exhausted

In local environment I changed 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 this on an online server.

I need to split this result before running the check, but I do not know how, can anyone help me?

    
asked by anonymous 26.08.2016 / 16:29

1 answer

0

It 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();

That would take (TAKE) 10 results after skipping 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 OFFSET or SKIP.

    
26.08.2016 / 16:53