2017 © Pedro Peláez
 

library eloquent-files

Eloquent files for easy management of your files for a model.

image

brysem/eloquent-files

Eloquent files for easy management of your files for a model.

  • Saturday, February 4, 2017
  • by Bryse Meijer
  • Repository
  • 2 Watchers
  • 3 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Eloquent Files

Packagist License Latest Stable Version Total Downloads, (*1)

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., (*2)

Installation

Require this package with composer:, (*3)

composer require brysem/eloquent-files

After updating composer, add the ServiceProvider to the providers array in config/app.php., (*4)

Bryse\Eloquent\Files\FilesServiceProvider::class,

Copy the package migrations to your local migrations folder with the publish command:, (*5)

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., (*6)

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., (*7)

// 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., (*8)

// 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., (*9)

// 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., (*10)

$file->isVideo()
$file->isImage()

The Versions

04/02 2017

dev-master

9999999-dev

Eloquent files for easy management of your files for a model.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Bryse Meijer

eloquent files

04/02 2017

v1.0.0

1.0.0.0

Eloquent files for easy management of your files for a model.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar Bryse Meijer

eloquent files