monstajamss's avatar

Image Upload Not Responding

So i am trying to upload image to 's3' but i am having problem saving the file name.. The image uploads to s3 but it saves as /private/var/tmp/phpRVAnrw in database.

Here is my controller

public function store(ThreadRequest $request)
    {
        $imageName = time().'.'.$request->attachment->extension();
        $request->attachment->storeAs('public/attachments', $imageName, 'wasabi');

        $this->dispatch(CreateThread::fromRequest($request, $imageName));
        return redirect()->route('threads.index');
    }

I have this in my Jobs

class CreateThread implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $title;
    private $body;
    private $category;
    private $author;
    private $attachment;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(string $title, string $body, string $category, User $author, string $attachment)
    {
        $this->title    = $title;
        $this->body     = $body;
        $this->category = $category;
        $this->author   = $author;
        $this->attachment = $attachment;
    }
    
    public static function fromRequest(ThreadRequest $request) : self
    {
        return new static(
            $request->title(), 
            $request->body(), 
            $request->category(), 
            $request->author(),
            $request->attachment()
        );
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(): Thread
    {
        $attachment = [
            'name' => $this->attachment->getClientOriginalName(),
            'url' => 'https://'.env('AWS_BUCKET').'.s3.'.env('AWS_DEFAULT_REGION').'.amazonaws.com/attachments/'.$this->attachment,
        ];

        $thread             = new Thread([
            'title'         => $this->title,
            'body'          => Purifier::clean($this->body),
            'category_id'   => $this->category,
        ]);

        $thread->authoredBy($this->author);
        $thread->save();

        return $thread;
    }
}

I am not sure what i am doing wrong, any help would be appreciated

I know i can't pass uploaded file to queue job but how do i solve this

0 likes
5 replies
Braunson's avatar
Braunson
Best Answer
Level 18

You can’t pass an uploaded file instance to a queued job.

The solution is to write the file to disk somewhere, and then retrieve it when handling the queued job.

monstajamss's avatar

I ended up doing this in my controller

$attachment = [];
      
            foreach(request()->attachment as $file){
                $attachment[] = [
                    'path' => $file->getRealPath(),
                    'name' => time().'.'.$file->extension(),
                    'attachment_url' => $file->store('public/attachments', 'wasabi'),
                ];
            }

        $this->dispatch(CreateThread::fromRequest($request, $attachment));
        return redirect()->route('threads.index');

and this in my Jobs and i am till getting error

foreach ($this->attachment as $requestImage) {
 

        $thread             = new Thread([
            'title'         => $this->title,
            'body'          => Purifier::clean($this->body),
            'category_id'   => $this->category,
            'attachment'    => $requestImage['path']
        ]);

        $thread->authoredBy($this->author);
        $thread->save();

        return $thread;
    }

Please or to participate in this conversation.