I need to upload a file . I want to separate the file with auto increment id in database so that whenever user delete or user upload same filename , it wont conflict . Here the code .
public function store( BannerFormRequest $request)
{
if($request->hasFile('image'))
{
$filename = $request->image->getClientOriginalName();
$request->image->move(public_path().'/banner/', $filename);
}
$banner = new Banner ([
'title'=> $request->get('title'),
'image'=> $filename
]);
$banner->save();
alert()->success('Success Message','Banner has been added!')->autoClose(3000);
return redirect('admin/list-banner');
}
does it have to be the autoincrement id? if not, and a unique id is enough, then i suggest using the out-of-the-box store() method for file upload, here is the documentation: https://laravel.com/docs/5.7/filesystem#file-uploads
and here is an example:
$path = $request->file('avatar')->store('avatars');
public function store( BannerFormRequest $request)
{
if($request->hasFile('image'))
{
//the path will contain a uniquely generated ID which serves as a file name
$path = $request->file('image')->store('banner'); //stores the file in the banners folder on your default public disk configured in config/filesystems.php
}
$banner = new Banner ([
'title'=> $request->get('title'),
'image'=> $path
]);
$banner->save();
alert()->success('Success Message','Banner has been added!')->autoClose(3000);
return redirect('admin/list-banner');
}
check the storage, do you have that linked? quote from the documentation:
Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.
I was unable to do it (i.e the storage with symbolic link, even if the symbolic link was created) work on my hosting (shared one), it's the reason why I use the "ugly custom manual" way I suggested you.
But what @s4muel is lot better than mine, but it seems not to work on all configuration.