PHPackages                             abdelrahmanbl/laravel-uploadable - 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. abdelrahmanbl/laravel-uploadable

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

abdelrahmanbl/laravel-uploadable
================================

this package adding behaviour to a model for self uploading images like avatar or any files type.

4.4(11mo ago)21818↑83.3%4PHP

Since Dec 7Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/AbdelrahmanBl/laravel-uploadable)[ Packagist](https://packagist.org/packages/abdelrahmanbl/laravel-uploadable)[ RSS](/packages/abdelrahmanbl-laravel-uploadable/feed)WikiDiscussions master Synced 1mo ago

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

laravel-uploadable
==================

[](#laravel-uploadable)

Laravel Uploadable for adding behaviour to a model for self uploading file like avatar or any file type.

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

[](#introduction)

This package can help you to upload image or any type of file to a specific destination in your filesystem, you can determine a path for a directory to save your uploaded file for each field in your table with minimal configurations or you can use the default store directory of the package.

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

[](#installation)

```
composer require abdelrahmanbl/laravel-uploadable

```

About Upload
------------

[](#about-upload)

This package uses the [Laravel File Storage](https://laravel.com/docs/11.x/filesystem) to keep the file management. The file will be stored inside the default disk. For example, if you are using the public disk, to access the image or file, you need to create a symbolic link inside your project:

```
php artisan storage:link

```

And then, configure your default filesystem from .env file

```
APP_URL=https://your-domain.com
FILESYSTEM_DISK=public # or your prefered disk

```

You can add `default_url` to the filesystems config file to overwrite the default file url. The default file url is asset('uploadable.jpg').

Usage
-----

[](#usage)

To use this package, import the FileCast in your model And then configure the $casts of your model with the FileCast class.

```
use Bl\LaravelUploadable\Casts\FileCast;

class User extends model
{
    /**
     * Don't forget
     * Add all the fields for file or image to the model $fillable when you don't use model $guarded.
     *
     * @var array
     */
    protected $fillable = [
        ...,
        'avatar',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        ...,
        'avatar' => FileCast::class,
    ];
}

```

Customize The Directory
-----------------------

[](#customize-the-directory)

```
'avatar' => FileCast::class . ':User/avatar', # this is the default value ( the attribute key name inside the model basename )

```

Customize The Disk
------------------

[](#customize-the-disk)

```
'avatar' => FileCast::class . ':default,s3', # here we customized the disk to s3.

```

Customize The Driver
--------------------

[](#customize-the-driver)

```
'avatar' => FileCast::class . ':default,default,' . CustomDriverService::class,

```

> ***Note:*** your customer driver service must implement `Bl\LaravelUploadable\Interfaces\UploadFileInterface`and has a constructor with parameter $disk

```
use Bl\LaravelUploadable\Interfaces\UploadFileInterface;
use Illuminate\Http\UploadedFile;

class CustomDriverService implements UploadFileInterface
{
    protected $disk;

    /**
     * __construct
     *
     * @param  \Bl\LaravelUploadable\Classes\FileArgument $disk
     * @return void
     */
    public function __construct($disk)
    {
        $this->disk = $disk->getValue();
    }

    /**
     * handle store proccess of the file.
     *
     * @param  UploadedFile $file
     * @param  string $directory
     * @return mixed
     */
    public function store(UploadedFile $file, string $directory): mixed
    {
        // ...
    }

    /**
     * handle getting the file full url path.
     *
     * @param  string $path
     * @return string
     */
    public function get(string $path): mixed
    {
        // ...
    }

    /**
     * handle deleting a file.
     *
     * @param  string $path
     * @return void
     */
    public function delete(string $path): void
    {
        // ...
    }
}

```

Customize The Default Path
--------------------------

[](#customize-the-default-path)

```
'avatar' => FileCast::class . ':default,default,default,null', # customize the default value to null.
'avatar' => FileCast::class . ':default,default,default,nullable', # customize the default value to null.
'avatar' => FileCast::class . ':default,default,default,avatar.png', # customize the default value to asset('avatar.png').

```

That's all! After this configuration, you can send file data from the client side with the same name of each file field of the model. The package will make the magic!

Example
-------

[](#example)

In frontend you can create a form-data with field name avatar.

```

    @csrf

        File

            Submit

```

In backend you can pass all the data to the User model.

```
/**
 * Handle the incoming request.
 */
public function store(UploadRequest $request)
{
    $user = \App\Models\User::query()->create(
        $request->validated() // or you can use $request->all() if you don't make a validation
    );

    // this get a link of the image that uploaded.
    $user->avatar; # https://domain.com/storage/User/avatar/U4q6En4mOHMJj0.png
}

```

You can update the file manually to the User model.

```
/**
 * Handle the incoming request.
 */
public function store(UploadRequest $request)
{
    $user = \App\Models\User::query()->first();
    $user->avatar = $request->file('avatar');
    $user->save();

    // this get a link of the image that uploaded.
    $user->avatar; # https://domain.com/storage/User/avatar/U4q6En4mOHMJj0.png
}

```

> ***Note:*** when update a field with a file the package will automatic delete the old file and put the new one.

Delete The File
---------------

[](#delete-the-file)

You can use the FileCastRemover trait in your model and when you deleting the model instance all the related files will be deleted automatically.

```

use Bl\LaravelUploadable\Traits\FileCastRemover;

class User extends Model
{
    use FileCastRemover;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'avatar',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'avatar' => FileCast::class,
    ];
}

```

And once the model instance is deleted all it's related files will be removed.

```
/**
 * Remove the user.
 */
public function destroy(User $user)
{
    $user->delete();
}

```

Apply The Events
----------------

[](#apply-the-events)

You can apply events either before or after the file upload. In addition to, you can apply that globally or for custom field.

### Global Events

[](#global-events)

- For apply global events before the file upload you should define a method called `beforeFileCastUpload` in your model and it take one paramter with type `\Illuminate\Http\UploadedFile` and return the same type.
- For apply global events after the file uploaded you should define a void method called `afterFileCastUpload` in your model and it take one paramter with type `\Illuminate\Http\UploadedFile`.

```
use Illuminate\Http\UploadedFile;

class User extends model
{
    /**
     * Apply before the file cast upload event.
     *
     * @param  UploadedFile $file
     * @return UploadedFile
     */
    public function beforeFileCastUpload(UploadedFile $file): UploadedFile
    {
        return $file;
    }

    /**
     * Apply after the file cast upload event.
     *
     * @param  UploadedFile $file
     * @return void
     */
    public function afterFileCastUpload(UploadedFile $file): void
    {
        dd($file);
    }
}

```

### Custom Events

[](#custom-events)

For apply custom events you should create a service that implement `Bl\LaravelUploadable\Interfaces\EventUploadInterface` and path it as a parameter.

```
'avatar' => FileCast::class . ':default,default,default,default,' . CustomUploadService::class

```

```
use Bl\LaravelUploadable\Interfaces\EventUploadInterface;
use Illuminate\Http\UploadedFile;

class CustomUploadService implements EventUploadInterface
{
    /**
     * Apply before the file cast upload event.
     *
     * @param  UploadedFile $file
     * @return UploadedFile
     */
    public function before(UploadedFile $file): UploadedFile
    {
        return $file;
    }

    /**
     * Apply after the file cast upload event.
     *
     * @param  UploadedFile $file
     * @return void
     */
    public function after(UploadedFile $file): void
    {
        dd($file);
    }
}

```

> ***Note:*** when applying global and custom events in your model the priority go to the custom event.

Contributing
------------

[](#contributing)

Feel free to comment, open issues and send PR's. Enjoy it!!

License
-------

[](#license)

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

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance52

Moderate activity, may be stable

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

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

Recently: every ~123 days

Total

10

Last Release

333d ago

Major Versions

1.0.0 → 2.02023-05-23

2.1 → 3.02023-10-28

3.1 → 4.02024-02-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/a3552279ed3a1586ed7ee9bb8fc2897bd901db36b9c00e67bee66672b4860023?d=identicon)[AbdelrahmanBl](/maintainers/AbdelrahmanBl)

---

Top Contributors

[![AbdelrahmanBl](https://avatars.githubusercontent.com/u/35730155?v=4)](https://github.com/AbdelrahmanBl "AbdelrahmanBl (56 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/abdelrahmanbl-laravel-uploadable/health.svg)

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

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[google/cloud-storage

Cloud Storage Client for PHP

34390.8M125](/packages/google-cloud-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15261.6M2.6k](/packages/illuminate-filesystem)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)

PHPackages © 2026

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