Here I am working under the impression that, the setup is ready, tables and migrations are there, form and view files are good to go.

We will focus here on the main uploading part, ones the file reach the controller. I am also skipping the validation part for the file as we will take it in consideration some other time.

So, as per me the best approach is to put the entire path of the file in database, instead of just putting the name of the uploaded file.

$fileLink = url(Storage::url(Storage::put('public/files', request('file'))));

The above piece of code will put the file in storage/app/public/files, with a uuid name and will return with the complete URL foe the file.

Before be able to access this file in browser by the provided link, you need to do one more thing, create the symlink from storage to public by running the following command

php artisan storage:link

and now we are good to go.


Giandomenico Di Salvatore commented on my post on Daily Dev and it is a great suggestion, we can use a memory-efficient approach like streaming the file to disk using Storage::putFile(). here is the link to the comment.

so now, the code will be updated to

$fileLink = url(Storage::url(Storage::putFile('public/files', request('file'))));