Allowed memory size of 134217728 bytes exhausted

Posted 2 years ago by kickthemooon

I am trying to import around 130k images via url. I thought it would be convenient to write a cli artisan command.

This is my script:

public function handle()
        Article::where('index_block_position', 0)->each(function($article){
            if($article->featured_image != ''){
                    $featuredImage = $this->mediaLibrary->addMediaFromUrl($article, '' . $article->featured_image, 'featured_image');
                    $libraryImage = $this->mediaLibrary->addMediaFromUrl(MediaLibrary::first(), '' . $article->featured_image);
                    $article->index_block_position = 1;
                    $this->info("Article {$article->id} image: {$featuredImage->id} - Library image: {$libraryImage->id} - added");
                    Log::info("Article {$article->id} image: {$featuredImage->id} - Library image: {$libraryImage->id} - added");
                }catch (\Exception $e){
                    $this->info("image: {$article->featured_image} - exception: {$e->getMessage()}");
        $this->info('All done!');

Unfortunately, after 10 or so images added I get the following error:

Symfony\Component\Debug\Exception\FatalErrorException: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24576 bytes)

I am not sure how i can make this work because i have 130k images to import and the memory limit of 128M is already exhausted after 10 images...

I would appreciate suggestions.

Please sign in or create an account to participate in this conversation.