Title: Allowed memory size of (888)671-4474 bytes exhausted
Tags: php;postgresql;laravel
Question: I encountering issue on my schedule on Laravel.
I see that there is duplicate questions about this error but these don't fix my issue
root@trafficshield:/var/www/vhosts/trafficshield.tools/httpdocs#
/opt/plesk/php/7.1/bin/php artisan schedule:run  Running scheduled
command: Closure ^[[15~PHP Fatal error:  Allowed memory size of
(888)671-4474 bytes exhausted (tried to allocate 4096 bytes) in
/var/www/vhosts/trafficshield.tools/httpdocs/vendor/laravel/framework/src/Illum
inate/Database/Eloquent/Model.php on line 279 PHP
This schedule aim to count all the visits of all campains on our service.
$schedule-&gt;call(function () {
$campaigns = Campaign75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ll();
foreach ($campaigns as $campaign) {
$campaign-&gt;denied_visits = $campaign-&gt;visitsDenied-&gt;count();
$campaign-&gt;allowed_visits = $campaign-&gt;visitsAllowed-&gt;count();
$campaign-&gt;save();
}
})-&gt;everyFiveMinutes();
How can I change the PHP code the avoid this issue ?
Config : memory_limit : 8G
Thank's in advance for your help.
Comment: To change the `memory_limit` would be a temporally fix when the ammount of data increase the problem come back so I would suggest you to avoid using Elloquent to do this job since Elloquent has poor memory efficiency. I would give you an approach hold on

Comment: @Greg I'm sure there is no infinite loop.

Comment: Just a food for thought: You often run out of memory if trapped in an infinite loop...

Here is the accepted answer: Allowed memory size of (888)671-4474 bytes exhausted
This kind of errors are caused by a big amount of data in memory, so the way to fix it is write a less heavy memory script. By changing memory_limit we only get a temporally fix because when our data grows it comes back.
$campaigns = Campaign75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ll(); //at this point you are pulling the whole Campaigns table to memory.. and you pull some extra rows after that increasing even more the memory use
As I said, Elloquent is poor efficient doing this kind of task so let's fetch the database rows as mysqli used to do, one by one:
$db = 75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0connection()-&gt;getPdo(); //get a database connection instance
$main_query_sql = "SELECT * FROM Campaigns"; //write our query
$main_query = $db-&gt;prepare($main_query_sql); //prepare it to get executed
$main_query-&gt;execute(); //execute our query
$visits_denied_sql = "SELECT COUNT(*) as total FROM VISITS WHERE DENIED = 1 AND CAMPAIGN_ID ="; //let's prepare our aggregate queries
$visits_allowed_sql = "SELECT COUNT(*) as total FROM VISITS WHERE DENIED = 0 AND CAMPAIGN_ID ="; //I just assumed them so change it as you need
$visits_denied = null;
$visits_allowed = null;
while($campaign = $main_query-&gt;fetch()){ //fetch our rows one by one
//now we are getting an associative array from the db instead of a model so we cannot use it as a Laravel Model (we can't use -&gt;save() method or eager loading)
$visits_denied = $db-&gt;prepare($visits_denied_sql.$campaign['id']);
$visits_denied = $visits_denied-&gt;execute();
$denied_visits = $visits_denied-&gt;fetch();
$visits_allowed= $db-&gt;prepare($visits_allowed_sql.$campaign['id']);
$visits_allowed= $visits_allowed-&gt;execute();
$allowed_visits = $visits_allowed-&gt;fetch();
$campaign['denied_visits'] = $denied_visits['total'];
$campaign['allowed_visits'] = $allowed_visits['total'] ;
//edit with the correct update sentence:
$insert_query_sql = "UPDATE CAMPAIGNS SET allowed_visits = :visits_allowed, denied_visits = :visits_denied WHERE id = :id";
$insert_query = $db-&gt;prepare($insert_query_sql);
$insert_query-&gt;bindParam(':visits_allowed', $campaign['allowed_visits']);
$insert_query-&gt;bindParam(':visits_denied', $campaign['denied_visits']);
$insert_query-&gt;bindParam(':id', $campaign['id']);
$insert_query-&gt;execute();
Try this inside your schedule and let me know if worked!
Comment for this answer: Did you import it like this? `use Illuminate\Support\Facades\DB;`
Comment for this answer: Maybe you have imported the incorrect one, I have a production project working with similar code. Check sql syntax maybe it is wrong. And use the `php artisan schedule:run -vvv` flag to get more info about the error
Comment for this answer: Yeah as I said, I just assumed your table structure and columns, fix the queries as you need
Comment for this answer: I have an error :
https://puu.sh/xAgsa/f248b8b553.png , I'm using Laravel 5.3 and PostgreSQL
Comment for this answer: Yes I imported it !
Comment for this answer: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154529/discussion-between-pixel-and-aaron0207).


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:09/15/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><postgresql><laravel>', 'Title': 'Allowed memory size of 8589934592 bytes exhausted', 'ViewCount': 1635, 'id': 46237168, 'user_AboutMe': None, 'user_AccountId': 6312345.0, 'user_DisplayName': 'Pixel', 'user_Reputation': 888.0, 'user_Views': 92.0, 'user_id': 4903508.0}}