You can install the package via composer:
composer require hamidrezaniazi/upolo
You can publish the migration with:
php artisan vendor:publish --provider="Hamidrezaniazi\Upolo\UpoloServiceProvider" --tag="migrations"
After publishing the migration you can create the files table by running the migrations:
php artisan migrate
You can persist uploaded files using the facade:
$file = Upolo::upload($uploadedFile)
If you want to add options in your file model during persisting, use this:
$file = Upolo::upload($uploadedFile, $user, $owner, $disk, $flag)
Owner is related to your file with a polymorphic relation and should implement from HasFileInterface and use the trait HasFileTrait like this:
<?php
class Owner extends Model implements HasFileInterface
{
use HasFileTrait;
Available filter scopes are:
//Filter by owner
Upolo::whereOwnerIs($owner)->get();
//Filter by owner id
Upolo::whereOwnerIdIs($ownerId)->get();
//Filter by owner type
Upolo::whereOwnerTypeIs($ownerType)->get();
//Filter by creator
Upolo::whereCreatorIs($creator)->get();
//Filter by creator id
Upolo::whereCreatorIdIs($creatorId)->get();
//Filter by flag
Upolo::whereFlagIs($flag)->get();
//Filter by type
Upolo::whereTypeIs($type)->get();
Type is filled automatically based on mime type and includes image, video, application, etc.
Also you can filter files via request in your controller. The keys bellow are available for filtering that you can send via query string:
- owner_id
- owner_type
- creator_id
- type
- flag
For request filtering the index function of your controller should be like this:
<?php
class FileController extends Controller
{
/**
* @param FileFilters $filters
* @return AnonymousResourceCollection
*/
public function index(FileFilters $filters): AnonymousResourceCollection
{
return FileResource::collection(File::filter($filters)->paginate());
}
Deleting file from storage and database is accessible with delete method:
$file->delete();
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email hamidrezaniazi@yahoo.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.