spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

with nova I want to uplpad a file to s3 to 'ly/contents', but it was created in the root dir. #3729

Open Eternal37 opened 11 hours ago

Eternal37 commented 11 hours ago

Image

            File::make(__('Attachment'), 'attachment')
                ->acceptedTypes('.pdf')
                ->disk('s3')
                ->path('ly/contents')
                ->store(function (Request $request, $model) {
                    $s3filePath = $request->attachment->store('ly/contents', 's3');
                    $model
                        ->addMediaFromDisk($s3filePath, 's3')
                        ->toMediaCollection('pdf');
                    return [
                        'attachment' => $s3filePath
                    ];
                })->onlyOnForms(),

# I have no attachment field in the database, so I use next fillFields function to fix this.

    // https://github.com/laravel/nova-issues/issues/2334#issuecomment-709422756
    protected static function fillFields(NovaRequest $request, $model, $fields)
        {
            $fillFields = parent::fillFields($request, $model, $fields);

            // first element should be model object
            $modelObject = $fillFields[0];

            // remove all attributes do not have relevant columns in model table
            unset($modelObject->attachment);

            return $fillFields;
        }