hyphen
hyphen

Reputation: 3450

laravel livewire uploaded file create object from path and save to s3

I'm uploading a file to a file upload component, and then passing the temporary file path to the parent component with an event. In the parent component I need to save the file to s3.

I need to pass the path or a file object or something back to the parent component, and then save it, but I can't seem to get it to work.

I've tried sending over a File object, as well as an UploadedFile object, my latest iteration is to try with a File object, and I'm getting the following error:

Unresolvable dependency resolving [Parameter #0 [ <required> string $path ]] in class Symfony\Component\HttpFoundation\File\File

So in my child component I have this code:

public function updatedFile()
{
    $fileObj = new File($this->file->path());
    $this->emitUp('fileUploaded', $fileObj);
}

In my parent component I'm listening for the fileUploaded event, which calls the save method:

    public function save(File $uploadedFile)
    {

        if ($path = Storage::putFileAs(env('APP_ENV') . '/statements', $uploadedFile->name, 's3')) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile->name,
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,

            ]);

            $this->statement->save();
        }
    }

I've also tried using $uploadedFile->storeAs() and I get the same result. It seems like the $uploadedFile object is not the right type. I don't know if I need a Storage object or what and I can't seem to find a good answer in the docs.

The path I have available after uploading the file in my livewire component is the temporary file name that livewire saves the file as in local storage. I also need the original file name as well, like what was uploaded as I'm saving that to the database.

If I remove the type hint on the save() method I get Attempt to read property "name" on array. Why is $uploadedFile an array and not an object? I guess if I remove the type hint it just gets sent over as an array. I dunno..

Upvotes: 0

Views: 2639

Answers (1)

hyphen
hyphen

Reputation: 3450

Here's the solution I came up with:

child component: public function updatedFile() { $this->validate([ 'file' => 'required|max:12288' ]);

        $this->emitUp('fileUploaded', [$this->file->path(), $this->file->getClientOriginalName()]);
    }

parent component:

public function save($uploadedFile)
    {
        if ($path = Storage::disk('s3')->put(env('APP_ENV') . '/statements/' . $uploadedFile[1], file_get_contents($uploadedFile[0]))) {
            $this->statement = new Statement([
                'location_id'   => $this->location->id,
                'file_name'     => $uploadedFile[1],
                'path'          => $path,
                'uploaded_by'   => Auth::user()->id,

            ]);

            $this->statement->save();
        }

    }

Upvotes: 0

Related Questions