PHPackages                             esign/laravel-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. esign/laravel-model-files

ActiveLibrary

esign/laravel-model-files
=========================

Associate files with your Laravel Models

1.8.0(1mo ago)011.8k↑13.9%MITPHPPHP ^8.1CI passing

Since Feb 7Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/esign/laravel-model-files)[ Packagist](https://packagist.org/packages/esign/laravel-model-files)[ Docs](https://github.com/esign/laravel-model-files)[ RSS](/packages/esign-laravel-model-files/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (12)Versions (10)Used By (0)

Associate files with your Laravel Models
========================================

[](#associate-files-with-your-laravel-models)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c674ca1c4588f05fb2323e77d52ffc34fd4fba955c2cd39baef9a0962461c245/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f657369676e2f6c61726176656c2d6d6f64656c2d66696c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/esign/laravel-model-files)[![Total Downloads](https://camo.githubusercontent.com/3ea87069bb5fec069afcc13c924142f1972452533d6bce4da783e739d8dc2208/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f657369676e2f6c61726176656c2d6d6f64656c2d66696c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/esign/laravel-model-files)[![GitHub Actions](https://github.com/esign/laravel-model-files/actions/workflows/main.yml/badge.svg)](https://github.com/esign/laravel-model-files/actions/workflows/main.yml/badge.svg)

This package allows you to store files for models in an opinionated way.

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

[](#installation)

You can install the package via composer:

```
composer require esign/laravel-model-files
```

Usage
-----

[](#usage)

### Preparing your model

[](#preparing-your-model)

To associate files with your model you need to use the `Esign\ModelFiles\Concerns\HasFiles` trait on the model.

```
use Esign\ModelFiles\Concerns\HasFiles;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFiles;
}
```

The database structure should look like the following:

```
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->boolean('file')->default(0);
    $table->string('file_filename')->nullable();
    $table->string('file_mime')->nullable();
});
```

### Configuring the disk

[](#configuring-the-disk)

By default, the files will be associated with the default disk configured in your `config/filesystems.php` file. You may override this by defining the `getFileDisk` method on your model.

```
use Esign\ModelFiles\Concerns\HasFiles;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFiles;

    public function getFileDisk(): string
    {
        return 'public';
    }
}
```

### Storing files

[](#storing-files)

To store files you may use the `storeFile` method. This method accepts instances of both the `Illuminate\Http\File` and `Illuminate\Http\UploadedFile` classes.

```
$post->storeFile('file', $request->file('attachment'));
```

To store raw string content, use the `storeFileFromString` method.

```
$post->storeFileFromString('file', 'Hello world');

$post->storeFileFromString('file', '{"key":"value"}');
```

### Retrieving file info

[](#retrieving-file-info)

```
$post->hasFile('file'); // returns true/false
$post->getFolderPath('file'); // returns posts/file
$post->getFilePath('file'); // returns posts/file/1.pdf
$post->getFilePathOnDisk('file'); // returns /path/to/storage/app/public/posts/file/1.pdf
$post->getFileMime('file'); // returns application/pdf
$post->getFileExtension('file'); // returns pdf
$post->getFileUrl('file'); // returns https://www.example.com/storage/posts/file/1.pdf
$post->getVersionedFileUrl('file'); // returns https://www.example.com/storage/posts/file/1.pdf?t=1675776047
```

### Deleting files

[](#deleting-files)

```
$post->deleteFile('file');
```

### Using with underscore translatable

[](#using-with-underscore-translatable)

This package ships with support for the [underscore translatable](github.com/esign/laravel-underscore-translatable) package.

Make sure to include the file, filename and mime columns within the `translatable` array:

```
use Esign\ModelFiles\Concerns\HasFiles;
use Esign\UnderscoreTranslatable\UnderscoreTranslatable;
use Illuminate\Database\Eloquent\Model;

class UnderscoreTranslatablePost extends Model
{
    use HasFiles;
    use UnderscoreTranslatable;

    public $translatable = [
        'document',
        'document_filename',
        'document_mime',
    ];
}
```

Next up, your migrations should look like the following:

```
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->boolean('document_en')->default(0);
    $table->boolean('document_nl')->default(0);
    $table->string('document_filename_en')->nullable();
    $table->string('document_filename_nl')->nullable();
    $table->string('document_mime_en')->nullable();
    $table->string('document_mime_nl')->nullable();
});
```

You may now use the internal methods using the default or the specific locale:

```
$post->hasFile('document'); // returns true/false
$post->getFolderPath('document'); // returns posts/document_en
$post->getFilePath('document'); // returns posts/document_en/1.pdf
$post->getFileMime('document'); // returns application/pdf
$post->getFileExtension('document'); // returns pdf
$post->getFileUrl('document'); // returns https://www.example.com/storage/posts/document_en/1.pdf
$post->getVersionedFileUrl('document'); // returns https://www.example.com/storage/posts/document_en/1.pdf?t=1675776047
```

```
$post->hasFile('document_en'); // returns true/false
$post->getFolderPath('document_en'); // returns posts/document_en
$post->getFilePath('document_en'); // returns posts/document_en/1.pdf
$post->getFileMime('document_en'); // returns application/pdf
$post->getFileExtension('document_en'); // returns pdf
$post->getFileUrl('document_en'); // returns https://www.example.com/storage/posts/document_en/1.pdf
$post->getVersionedFileUrl('document_en'); // returns https://www.example.com/storage/posts/document_en/1.pdf?t=1675776047
```

### Testing

[](#testing)

```
composer test
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance89

Actively maintained with recent releases

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~142 days

Recently: every ~122 days

Total

9

Last Release

56d ago

PHP version history (2 changes)1.0.0PHP ^8.0

1.6.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4599d7a8f6fdb63dd04305a49ae5ec9700b7a6eacdbe3a54f89584d75e34503f?d=identicon)[esign](/maintainers/esign)

---

Top Contributors

[![jordyvanderhaegen](https://avatars.githubusercontent.com/u/24370626?v=4)](https://github.com/jordyvanderhaegen "jordyvanderhaegen (32 commits)")

---

Tags

esignmodel-files

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/esign-laravel-model-files/health.svg)

```
[![Health](https://phpackages.com/badges/esign-laravel-model-files/health.svg)](https://phpackages.com/packages/esign-laravel-model-files)
```

###  Alternatives

[esign/laravel-conversions-api

A laravel wrapper package around the Facebook Conversions API

69145.4k](/packages/esign-laravel-conversions-api)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
