PHPackages                             ssntpl/laravel-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. [File &amp; Storage](/categories/file-storage)
4. /
5. ssntpl/laravel-files

ActiveLibrary[File &amp; Storage](/categories/file-storage)

ssntpl/laravel-files
====================

Associate files with Eloquent models

v0.1.6(7mo ago)111.1k[2 PRs](https://github.com/ssntpl/laravel-files/pulls)2MITPHPPHP ^7.4|^8.0

Since Jun 23Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/ssntpl/laravel-files)[ Packagist](https://packagist.org/packages/ssntpl/laravel-files)[ Docs](https://github.com/ssntpl/laravel-files)[ RSS](/packages/ssntpl-laravel-files/feed)WikiDiscussions master Synced today

READMEChangelog (7)Dependencies (1)Versions (13)Used By (2)

Associate files with Eloquent models
====================================

[](#associate-files-with-eloquent-models)

[![Latest Version on Packagist](https://camo.githubusercontent.com/fd02f77a2f5143db64f844da41c063f3f2354b5285c63707b0512df19f46f1d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73736e74706c2f6c61726176656c2d66696c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ssntpl/laravel-files)[![Total Downloads](https://camo.githubusercontent.com/d5d0a7fd5ee946b347167be3cc78825dca40f501b94301e3fd7e4aafc974aa5a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73736e74706c2f6c61726176656c2d66696c65732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ssntpl/laravel-files)

This is a simple package to associate files with your eloquent model in laravel.

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

[](#installation)

You can install the package via composer:

```
composer require ssntpl/laravel-files
```

Run the migrations with:

```
php artisan migrate
```

Optionally, You can publish and run the migrations with:

```
php artisan vendor:publish --tag="laravel-files-migrations"
php artisan migrate
```

You can publish the config file with:

```
php artisan vendor:publish --tag="laravel-files-config"
```

This is the contents of the published config file:

```
return [
    /*
     * When using the "HasFiles" trait from this package, we need to know which
     * Eloquent model should be used to retrieve your files. Of course, it
     * is often just the "File" model but you may use whatever you like.
     *
     * The model you want to use as a File model needs to implement the
     * `Ssntpl\LaravelFiles\Contracts\File` contract.
     */

    'model' => Ssntpl\LaravelFiles\Models\File::class,

    /*
     * When using the "HasFiles" trait from this package, we need to know which
     * table should be used to retrieve your files. We have chosen a basic
     * default value but you may easily change it to any table you like.
     */

    'table' => 'files',

    /*
     * Define the hashing algorithm to calculate the checksum of the file content.
     * Most commonly used hashing algorithm are md5, sha1, sha256. For a complete
     * list of supported hashing algorithms, check hash_algos().
     */

    'checksum' => 'md5',
];
```

Usage
-----

[](#usage)

Add the `HasFiles` trait to your model.

```
use Illuminate\Foundation\Auth\User as Authenticatable;
use Ssntpl\LaravelFiles\Traits\HasFiles;

class User extends Authenticatable
{
    use HasFiles;
}
```

Add new file to the model.

```
$model = User::find(1);

$file = $model->createFile([

    // type: Optional. If missing, the deault type of the model is used.
    'type' => 'avatar',

    // name: Optional. Represents the file name. If missing, a random uuid is generated.
    'name' => 'my_avatar.png',

    // key: Optional. If missing the key is auto-generated from FilePrefix attribute and name of the file.
    'key' => 'avatar/user/1/my_avatar.png',

    // disk: Optional. Represents the disk on which the file is stored. If missing the default disk is used.
    'disk' => 's3',

    // base64: Optional. If present the string is decoded and written to disk.
    'base64' => 'base64 encoded string of the content of the file.',

    // contents: Optional. base64 takes precedence over contents key. If both base64 and contents key are missing then you can add the contents to the file later.
    'contents' => 'you can directly provide the contents instead of the base64 string',
]);
```

Defining the FilePrefix for the model.

```
class Flight extends Model
{
    HasFiles;

    Protected $file_prefix = 'flights';
}
```

Defining FilePrefix dynamically. Sometimes we don't need a fixed path to store all the files of the model. You can use the FilePrefix attribute to create dynamic file prefix.

```
class Flight extends Model
{
    HasFiles;

    public function getFilePrefixAttribute()
    {
        return 'flights/' . $this->id;
    }
}
```

Accessing the File model.

```
$file->url; // Returns the url() of the file.

$file->exists(); // Boolean. Checks if the file exists on the disk.

echo $file; // Prints the url of the file.

echo $file->base64; // Prints the base64 encoded string of the file contents.

$file->base64 = 'base64 contents'; // Writes the content to the disk.

echo $file->contents; // Print the file contents.

$file->contents = 'file contents'; // Writes the contents to the disk.
```

Fetch single file. An excpeption is thrown if none, or more than one file is available.

```
$model->file;           // Fetches single file of default type.

$model->file();         // Fetches single file of default type.

$model->file($type);    // Fetches single file of $type type.
```

Fetch multiple file relation.

```
$files = $model->files();   // Fetches relation to all the files belonging to the model.

$files->get(); // Fetches all the files of any type that belong to the model.

$files = $model->files($type);   // Fetches relation to all the files of type $type that belong to the model.

$files->get(); // Fetches all the files of type $type.
```

Testing
-------

[](#testing)

```
composer test
```

TODO
----

[](#todo)

- Declare the `Ssntpl\LaravelFiles\Contracts\File` contract
- Add option to define default disk for every model
- Add path() method that returns the full path of the file
- Make File model sub-class of `Illuminate\Http\File` and see if all the methods work fine.
- See if destroy/delete method can be modified in trait to handle the file objects

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Sam](https://github.com/ssntpl)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance63

Regular maintenance activity

Popularity25

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~207 days

Recently: every ~96 days

Total

7

Last Release

227d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6224763?v=4)[Sword Software N Technologies Pvt. Ltd.](/maintainers/ssntpl)[@ssntpl](https://github.com/ssntpl)

---

Top Contributors

[![JYOTSANASHARMAA](https://avatars.githubusercontent.com/u/116160861?v=4)](https://github.com/JYOTSANASHARMAA "JYOTSANASHARMAA (15 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (10 commits)")[![sambhav-aggarwal](https://avatars.githubusercontent.com/u/4591834?v=4)](https://github.com/sambhav-aggarwal "sambhav-aggarwal (8 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (7 commits)")

---

Tags

eloquent-modelsfileslaravellaravel-filesssntpllaravelfileslaravel-filesssntpl

###  Code Quality

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[masterro/laravel-file-cleaner

Laravel console command for deleting temp files and associated model instances.

197.1k](/packages/masterro-laravel-file-cleaner)[gaspertrix/laravel-backpack-dropzone-field

Add Dropzone support for Laravel Backpack

172.3k](/packages/gaspertrix-laravel-backpack-dropzone-field)

PHPackages © 2026

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