Downloads
Stars
Version
Laravel package for easy file uploads maintenance. It helps to maintain file upload on each of your model.
What this packages do?
Storage
, no need path config, respecting your config/filesystem.php
upload_*
mutator and accessor.uploads
mutator and accessorInstall the package via Composer
composer require frengky/yupload
Then publish the configuration files to your app/config
php artisan vendor:publish --tag=config
Finally, run migrations to create the uploads
table
php artisan migrate
For each of your model that have file uploads, use the HasUploads
traits
use Frengky\Yupload\Concerns\HasUploads;
class User extends Authenticatable
{
use HasUploads;
// store uploaded files under 'user/' path
}
class Product extends Model
{
use HasUploads;
// store upload under 'product/' path
}
The physical file will be stored using
Storage
to your desired disk. It will using your model name (in lowercase) as path.
Saving uploaded file via uploads
and upload_*
mutators
$user = User::find(1);
// Store uploaded file via dynamic mutator
$user->upload_photo = $request->file('photo');
$user->upload_screenshot = $request->file('screenshot');
$user->uploads = $request->file('all_documents');
// via create
$newUser = User::create([
'name' => 'Foo',
'email' => '[email protected]',
'upload_photo' => $request->file('photo')
]);
// store or update via fill
$user->fill([
'upload_photo' => $request->file('photo')
]);
// store via relationship
$user->uploads()->save(
Upload::make($request->file('photo'))
);
// Store multiple files via predefined 'uploads' mutators
$user->uploads = $request->files;
$user->uploads = $request->file('anotherfile1');
$user->uploads = $request->file('anotherfile2');
// Finally call save() your model as usual to save the related uploaded files
$user->save();
All
upload_*
prefixed attributes are virtual mutator, creating database fields for each upload type is not needed. Theuploads
attribute are predefined mutator to store multiple files.
Acessing uploaded file via uploads
and upload_*
accessor
$product = Product::find(1);
// via accessor
$photo = $product->upload_photo;
// via relationship
$photo = $product->uploads()->ofType('photo');
// All uploaded file for this entity
$allFiles = $product->uploads;
$product = Product::find(1);
// via accessor
$product->upload_photo = $request->file('photo');
$product->save();
// via 'Upload' model
$photo = $product->uploads()->ofType('photo');
$photo->file = $request->file('photo');
$photo->save();
The previous physical file will be automatically deleted on save()
// Delete single uploaded file
$photo = $user->upload_photo;
$photo->delete();
// Delete all uploaded file related to this entity
$user->deleteUploads();
// Deleting the entity also delete all related uploaded file
$user->delete();
If your model use SoftDeletes, then the uploaded file will be preserved, only deleted on forceDelete();
That's all for now.