I just upgraded my homestead to the latest version and now a script I've been running successfully is now getting the error:
PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/vagrant/Code/app1/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 458
[Symfony\Component\Debug\Exception\FatalErrorException]
Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes)
The previous version of homestead worked fine and my production server works fine. Why am I suddenly getting this error?
I know I can just up the memory limit of PHP but I am trying to avoid that if I can because it was working fine before. Current memory limit is set to 512MB.
Here is the script I'm running, is there anything wrong with it?
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Metric;
use App\Location;
use League\Csv\Reader;
use League\Csv\Statement;
class ImportMetrics extends Command
{
protected $signature = 'import:metrics';
protected $description = 'Import metrics from CSV';
public function __construct()
{
parent::__construct();
}
public function handle()
{
if (file_exists($path = storage_path('app/import.csv'))) {
Metric::truncate();
$locations = Location::all();
$csv = Reader::createFromPath($path);
$records = (new Statement())->offset(1)->process($csv);
$bar = $this->output->createProgressBar(count($records));
foreach ($records as $record) {
$record = array_map('filter', $record);
$metric = new Metric([
'product_code' => $record[7],
'product_group' => $record[3],
'product_subgroup' => $record[4],
'product_class' => $record[5],
'product_subclass' => $record[6],
'unit' => $record[11],
'previous_quantity' => number($record[16]),
'previous_mt' => number($record[39]),
'previous_50lb' => number($record[37]),
'previous_sales' => number($record[15]),
'previous_gp' => number($record[36]),
'current_quantity' => number($record[10]),
'current_mt' => number($record[40]),
'current_50lb' => number($record[38]),
'current_sales' => number($record[9]),
'current_gp' => number($record[30]),
]);
$metric->location()->associate(
$locations->where('code', $record[0])->first()
);
$metric->save();
$bar->advance();
}
$bar->finish();
}
}
}