Skip to content

brysem/eloquent-files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Eloquent Files

Packagist License Latest Stable Version Total Downloads

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()

About

Attaching files to eloquent models made easy.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages