PHPackages                             cyber-duck/model-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. cyber-duck/model-files

ActiveLibrary

cyber-duck/model-files
======================

Model HasFiles Trait for Eloquent Models

00PHP

Since Oct 26Pushed 4y ago22 watchersCompare

[ Source](https://github.com/Cyber-Duck/Laravel-Model-Files)[ Packagist](https://packagist.org/packages/cyber-duck/model-files)[ RSS](/packages/cyber-duck-model-files/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

🚨 Discontinued 🚨
================

[](#-discontinued-)

Please consider using [Spatie Media Library](https://github.com/spatie/laravel-medialibrary) instead.

Model Files
===========

[](#model-files)

This is still in development, since it was extracted from an existing project some functionality is still being developed and changed.

The tests were linked to multiple Models (will be moved soon).

Author: [Tiago Tavares](https://github.com/tiagocyberduck)

Made with ❤️ by [Cyber-Duck Ltd](http://www.cyber-duck.co.uk)

Introduction
------------

[](#introduction)

Model files provide a simple trait (`HasFiles`) to be used by your eloquent models.

This trait allows you to define in your model through an array a simple storage configuration:

- An observer will handle the storage and pruning of files presents in the disk.
- Another trait is used for methods interacting with the Storage facade.

Installation
------------

[](#installation)

```
composer require cyber-duck/model-files --dev

```

Usage
-----

[](#usage)

Storing/Saving e.g. from one of your controller:

```
...
public function store(Request $request, Company $company) {
    $validatedData = $request->validate([
        'logo' => 'required|image',
    ]);

    // You can set logo to a string (if it's already stored in the disk)
    // If won't create another file
    $company->logo = $validatedData->logo;
    $company->save();

    ...
}
```

Accessing the file url:

```
Company::find(1)->url('logo');
```

Delete file:

```
$company = Company::find(1);
$company->deleteFile('logo'); // Deletes and sets the attribute to `null`
$company->save();
```

@todo delete model also prunes the file

```
Company::find(1)->delete(); // File will be deleted as well
```

\### Configuration

Make your model implement Storable interface and use the trait `HasFiles` to your models class.

```
