PHPackages                             brysem/eloquent-files - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. brysem/eloquent-files

ActiveLibrary[Database &amp; ORM](/categories/database)

brysem/eloquent-files
=====================

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

v1.0.0(9y ago)324MITPHPPHP &gt;=5.6.4

Since Feb 4Pushed 9y ago2 watchersCompare

[ Source](https://github.com/brysem/eloquent-files)[ Packagist](https://packagist.org/packages/brysem/eloquent-files)[ RSS](/packages/brysem-eloquent-files/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (5)Versions (2)Used By (0)

Eloquent Files
--------------

[](#eloquent-files)

[![Packagist License](https://camo.githubusercontent.com/e1ed7746daa3c1f66cb7f342b1a46f7c4695f39bc55bdaa52d93f3cd26fb2fd4/68747470733a2f2f706f7365722e707567782e6f72672f62727973656d2f656c6f7175656e742d66696c65732f6c6963656e73652e706e67)](http://choosealicense.com/licenses/mit/)[![Latest Stable Version](https://camo.githubusercontent.com/a171df609a8e0a001d8df180922e04d2e43d8c37619b4136c708381064e77fea/68747470733a2f2f706f7365722e707567782e6f72672f62727973656d2f656c6f7175656e742d66696c65732f76657273696f6e2e706e67)](https://packagist.org/packages/brysem/eloquent-files)[![Total Downloads](https://camo.githubusercontent.com/287bf9f4247fec06ca165d02e58be3d6c7aef8efd780d6dcea8951ada61a8cae/68747470733a2f2f706f7365722e707567782e6f72672f62727973656d2f656c6f7175656e742d66696c65732f642f746f74616c2e706e67)](https://packagist.org/packages/brysem/eloquent-files)

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

[](#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
-----

[](#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', '
