Home > Mobile >  Laravel queues Call to a member function storeAs() on string
Laravel queues Call to a member function storeAs() on string

Time:03-28

I am trying to store heavy image through the laravel queue as follows because i dont want my user to wait until file gets stored:

This code is in controller

if($request->hasfile('featuringPhoto')) 
        {
            $prefixFilename = date("Y-m-d-H-i-s");
            $coverFilename = $prefixFilename.$request->featuringPhoto->getClientOriginalName();
            
            ProceessFiles::dispatch($request->featuringPhoto->getRealPath(),$coverFilename);
            
        }
        else
        {
            $coverFilename4 = NULL;
        }

Below code is in job

protected $files, $filename;

public function __construct($files,$filename)
    {
        $this->files= $files;
        $this->filename = $filename;
    }

public function handle()
    {
        if($this->files){
            
         
          $coverFilename = $prefixFilename.$this->filename;
          $img = file_get_contents($this->files);
    
          $img->storeAs('images/photosUploadedByUser', $coverFilename, 'public');
      
        }
    }

It Gives me an error saying Call to a member function storeAs() on string I tried this solution from stack but didnt work

Any suggestion will be appreciated.Thank you.

CodePudding user response:

I think it is wrong to assume you are gonna save a lot time by executing the save operation in a queue, also because you are already fetching it from the web server. Queues will with scaling often be moved to worker servers and with this approach this will not work.

In the spirit of the question, stackoverflow and to explain to you what is not working. file_get_contents() returns the content of the file as a string. So to fix your problem, you should just store the results of that. You obviously can not call methods on strings.

$coverFilename = $prefixFilename.$this->filename;
$img = file_get_contents($this->files);
Storage::put('images/photosUploadedByUser/' . $coverFilename, $img);
  • Related