brysem / eloquent-files
Eloquent files for easy management of your files for a model.
Requires
- php: >=5.6.4
- illuminate/filesystem: 5.4.*
- illuminate/support: 5.4.*
Requires (Dev)
- fzaninotto/faker: ~1.4
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~5.0
This package is not auto-updated.
Last update: 2024-11-10 02:49:16 UTC
README
This is a package to make uploading and attaching files to an eloquent model easy.
It includes a ServiceProvider to publish the migrations.
File uploads will be processed by your default filesystem storage. This can be changed in the filesystem.php
config.
When deleting a model with files attached, the files will automatically also be deleted.
Installation
Require this package with composer:
composer require brysem/eloquent-files
After updating composer, add the ServiceProvider to the providers array in config/app.php
.
Bryse\Eloquent\Files\FilesServiceProvider::class,
Copy the package migrations to your local migrations folder with the publish command:
php artisan vendor:publish --provider="Bryse\Eloquent\Files\FilesServiceProvider" --tag="migrations"
Add the HasFiles
trait to the eloquent models you would like to save files to.
use Bryse\Eloquent\Files\Traits\HasFiles; class User extends Authenticatable { use HasFiles; }
Usage
You can now easily process file uploads and save them to your eloquent models.
// Returns an array of the files that have been uploaded. // The second parameter is the path inside your storage_path(). $user->upload(request()->file(), 'users');
You also have access to some useful relationships.
// Get a collection (array) of files that belong to the model. $files = $user->files; // Get a collection of image files that belong to the model. $images = $user->files()->images()->get(); // Get a collection of video files that belong to the model. $videos = $user->files()->videos()->get(); // You can go crazy and grab all text files created three days ago. $files = $user->files()->where('created_at', '<=', Carbon\Carbon::now()->subDays(3))->where('type', 'like', 'text/%')->get();
Easily add a profile image for users.
// UserController.php public function uploadProfileImage(Request $request, User $user) { // Remove all previous images. $user->files->each(function($file) { $file->delete(); }); $user->upload(request()->file(), 'users'); } // User.php public function getImageAttribute() { return $this->files()->images()->first()->url; } // Usage <img src="{{ $user->image }}">
Check if a file is an image or video with these easy helpers.
$file->isVideo() $file->isImage()