简体   繁体   中英

Laravel Nova storing original file name in the database and in local storage

I'm having an issue with storing the original file name both in the database and in disk storage. The file saves correctly in the database and in the Nova backend, but in the disk storage is just saves as a random string.

In the database: my-user-friendly-file-name.pdf

In the disk storage: kfnakfncanjnaskdmkasniodanwjioeocniosandoaisndcacs.pdf

I have followed the docs, and it seems the below code should work but it doesn't.

File::make('Attachment PDF Upload', 'attachment_url')
    ->rules('file')
    ->disk('attachments')
    ->storeOriginalName('attachment_url')
    ->storeAs(function(Request $request) {
        return sha1($request->attachment_url->getClientOriginalName()) . '.' . pathinfo($request->attachment_url->getClientOriginalName(), PATHINFO_EXTENSION);
}),

Has anyone faced a similar issue?

So just as @ceejayoz said I ended up storing an extra column for the original file name and then call that in your code where needed, in my case emailing out attachments.

File::make('Attachment PDF Upload', 'attachment_url')
    ->store(function (Request $request, $model) {
        return [
            'attachment_url' => $request->attachment_url->store('/', 'attachments'),
            'attachment_original_name' => $request->attachment_url->getClientOriginalName(),
        ];
    }),

Then simply attach like this:

$message->attach($attachment->attachment_url, ['as' => $attachment->attachment_original_name]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM