The recommended way to install File Package is through Composer.
composer require bnhashem/fileCreating Shourtcut of storage folder , you will found shortcut by following this path your-project/public
php artisan storage:linkyou should Go to config\app.php , in Provider array you should put this Line:
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Bnhashem\File\FileServiceProvider::class,
], use Bnhashem\File\Traits\File;
public function store(Request $request)
{
$post = new Post([
'image' => File::store('posts' , 'image'),
'banner' => File::store('posts' , 'banner'),
]);
$post->save();
// posts is the parent Folder
// image and banner are the childs Folders
// image and banner also request name , that mean image or banner is required.
}In update Model function:
- If
$request->imageor$request->banneris Equal null , that mean Nothing Will Not happen - If
$request->imageor$request->banneris Not Equal null , that mean image or banner will removed from Database and Server and store New image that will comming From request
use Bnhashem\File\Traits\File;
public function update(Request $request , Post $post)
{
$post->update([
'image' = File::update('posts' , 'image' , $post),
'banner' = File::update('posts' , 'banner' , $post),
]);
// posts are the parent Folder
// image and banner are the childs Folders
// image and banner also request name , that mean image or banner is required.
// You should pass Row to function
}In destroy Model function: File will removed From Database and also will Removed from server.
use Bnhashem\File\Traits\File;
public function destroy(Request $request , Post $post)
{
File::destroy('image' , $post);
File::destroy('banner' , $post);
// You should pass Row to function
}For Example if you upload photo , you will find it According to this path :
<img src="{{ url('storage/'.$post->image) }}" >
<img src="{{ url('storage/'.$post->banner) }}" >