PHPackages                             kolirt/laravel-master-model - 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. [Framework](/categories/framework)
4. /
5. kolirt/laravel-master-model

ActiveLibrary[Framework](/categories/framework)

kolirt/laravel-master-model
===========================

The package that simplifies working with models, saving relationships, and uploading files in Laravel

4.0.10(1y ago)31.2k↓63.6%[1 issues](https://github.com/kolirt/laravel-master-model/issues)1MITPHPPHP &gt;=8.1

Since Jul 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/kolirt/laravel-master-model)[ Packagist](https://packagist.org/packages/kolirt/laravel-master-model)[ Docs](https://github.com/kolirt/laravel-master-model)[ Fund](https://www.buymeacoffee.com/kolirt)[ RSS](/packages/kolirt-laravel-master-model/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (1)Versions (16)Used By (1)

Laravel Master Model
====================

[](#laravel-master-model)

Laravel Master Model is a powerful package for the Laravel framework that simplifies working with models, particularly in **saving relations** and **uploading files**.

This package is designed for developers who want to optimize the process of working with databases and files, reducing code complexity and enhancing performance.

Structure
---------

[](#structure)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Installation](#installation)
    - [Setup](#setup)
- [Console commands](#console-commands)
- [Use cases](#use-cases)
    - [Saving files](#saving-files)
    - [Saving files from third-party resources](#saving-files-from-third-party-resources)
    - [Deleting files](#deleting-files)
    - [Saving `HasOne`, `MorphOne` relations](#saving-hasone-morphone-relations)
    - [Saving `HasMany`, `MorphMany` relations](#saving-hasmany-morphmany-relations)
    - [Saving `HasMany`, `MorphMany` relations with `sync` mode](#saving-hasmany-morphmany-relations-with-sync-mode)
    - [Saving `BelongsToMany` relation](#saving-belongstomany-relation)
    - [Saving `BelongsToMany` relation with `sync` mode](#saving-belongstomany-relation-with-sync-mode)
    - [Response file](#response-file)
- [FAQ](#faq)
- [License](#license)
- [Other packages](#other-packages)

[ ![Buy Me A Coffee](https://camo.githubusercontent.com/a6ebf9f3a5d0689c6d7449b0ea0c4ce75bbff5ef09b38babf991cb7a559f8774/68747470733a2f2f63646e2e6275796d6561636f666665652e636f6d2f627574746f6e732f76322f617269616c2d79656c6c6f772e706e67)](https://www.buymeacoffee.com/kolirt)Getting started
---------------

[](#getting-started)

### Requirements

[](#requirements)

- PHP &gt;= 8.1
- Laravel &gt;= 10

### Installation

[](#installation)

```
composer require kolirt/laravel-master-model
```

### Setup

[](#setup)

Publish config file

```
php artisan master-model:install
```

Use the `MasterModel` trait in your models

```
use Kolirt\MasterModel\MasterModel;

class Item extends Model
{
    use MasterModel;
}
```

Console commands
----------------

[](#console-commands)

- `master-model:install` - Install master model package
- `master-model:publish-config` - Publish the config file

Use cases
---------

[](#use-cases)

### Saving files

[](#saving-files)

```
class Item extends Model
{
    use MasterModel;

    protected $fillable = [
        'image',
    ];
}
```

MasterModel automatically saves the file and deletes the old file, if it existed

```
class ExampleController extends Controller
{
    public function index(Request $request, $id)
    {
        $data = $request->validate([
            'image' => 'required|file',
        ]);

        $item = Item::query()->findOrFail($id);
        $item->update($data);
    }
}
```

You can specify folder and disk for each file

```
class Item extends Model
{
    use MasterModel;

    protected $fillable = [
        'image',
    ];

    protected string $upload_model_folder = 'items';

    protected array $upload_folders = [
        'image' => 'image',
    ];

    protected array $upload_disks = [
        'image' => 'public'
    ];
}
```

### Saving files from third-party resources

[](#saving-files-from-third-party-resources)

You no longer need to worry about saving files from third-party resources, just put the `response` and MasterModel will save everything for you

```
class ExampleController extends Controller
{
    public function index($id)
    {
        $file1_url = 'https://png.pngtree.com/png-clipart/20230126/original/pngtree-fresh-red-apple-png-image_8930987.png';
        $response = \Illuminate\Support\Facades\Http::get($file1_url);

        $file2_url = 'https://cubanvr.com/wp-content/uploads/2023/07/ai-image-generators.webp';
        $client = new \GuzzleHttp\Client();
        $response2 = $client->get($file2_url);

        Item::create([
            'image' => $response,
            'image2' => $response2
        ]);
    }
}
```

### Deleting files

[](#deleting-files)

You can delete files by setting the field to `null`

```
$item = Item::query()->first();

$item->update([
    'image' => null
]);
```

To have files deleted automatically, delete data through the model, not through the builder, and don't forget to load the necessary relations in which you want to delete files

*If there are files in the relationship and the relationship is deleted not through the model, the files won't be deleted and will clog up storage*

```
$item = Item::query()->with(['phone', 'addresses'])->first();
/**
* All files in the model and in the loaded relations will be deleted
 */
$item->delete();
```

### Saving `HasOne`, `MorphOne` relations

[](#saving-hasone-morphone-relations)

You can **save** `HasOne`, `MorphOne` relations in the same way as a file. If relation exists, it will be updated, otherwise it will be created

```
$item = Item::query()->first();

$item->update([
    'phone' => [ // hasOne, morphOne relation
        'number' => '1234567890'
    ]
]);
```

You can also **delete** the relation by setting it to `null`

```
$item = Item::query()->first();

$item->update([
    'phone' => null // hasOne, morphOne relation
]);
```

### Saving `HasMany`, `MorphMany` relations

[](#saving-hasmany-morphmany--relations)

You can **save** `HasMany`, `MorphMany` relations in the same way as a file. If relations exists, it will be updated, otherwise it will be created

```
$item = Item::query()->first();

$item->update([
    'phones' => [ // hasMany, morphMany relations
        [ // will be created
            'number' => '1234567890'
        ],
        [ // will be updated (id = 1)
            'id' => 1,
            'number' => '0987654321'
        ]
    ]
]);
```

### Saving `HasMany`, `MorphMany` relations with `sync` mode

[](#saving-hasmany-morphmany-relations-with-sync-mode)

You can also **sync** `HasMany`, `MorphMany` relations. Unspecified relations will be deleted

```
$item = Item::query()->first();

$item->update([
    'phones' => [ // hasMany, morphMany relations
        'mode' => 'sync', // not specified relations will be deleted
        'value' => [
            [ // will be created
                'number' => '1234567890'
            ],
            [ // will be updated (id = 1)
                'id' => 1,
                'number' => '0987654321'
            ]
        ]
    ]
]);
```

### Saving `BelongsToMany` relation

[](#saving-belongstomany-relation)

```
$item = Item::query()->first();

$item->update([
    'categories' => [1, 2, 3] // belongsToMany relations
]);

$item->update([
    'categories' => [ // belongsToMany relation
        1 => ['name' => 'Category 1'],
        2 => ['name' => 'Category 2'],
        3 => ['name' => 'Category 3']
    ]
]);
```

### Saving `BelongsToMany` relation with `sync` mode

[](#saving-belongstomany-relation-with-sync-mode)

You can **sync** the `BelongsToMany` relation. Everything that is not specified when saving will be deleted

```
$item = Item::query()->first();

$item->update([
    'categories' => [ // belongsToMany relation
        'mode' => 'sync', // not specified relations will be deleted
        'value' => [1, 2, 3]
    ]
]);

$item->update([
    'categories' => [ // belongsToMany relation
        'mode' => 'sync', // not specified relations will be deleted
        'value' => [
            1 => ['name' => 'Category 1'],
            2 => ['name' => 'Category 2'],
            3 => ['name' => 'Category 3']
        ]
    ]
]);
```

### Response file

[](#response-file)

Use the `responseFile` method to return a file in a controller

```
class FileController extends Controller
{

    public function index()
    {
        $item = Item::query()->first();
        return $item->responseFile('image');
    }

}
```

FAQ
---

[](#faq)

Check closed [issues](https://github.com/kolirt/laravel-master-model/issues) to get answers for most asked questions

License
-------

[](#license)

[MIT](LICENSE.txt)

Other packages
--------------

[](#other-packages)

Check out my other packages on my [GitHub profile](https://github.com/kolirt)

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance42

Moderate activity, may be stable

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

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

Recently: every ~59 days

Total

15

Last Release

374d ago

Major Versions

2.0.2 → 3.0.22023-09-06

3.1.3 → 4.0.02024-09-07

### Community

Maintainers

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

---

Top Contributors

[![kolirt](https://avatars.githubusercontent.com/u/11573343?v=4)](https://github.com/kolirt "kolirt (26 commits)")

---

Tags

laravel-file-managerlaravel-file-uploadlaravel-modellaravel-packagelaravel-relationshipframeworklaravelfilesimagesuploadautorelationsfill

### Embed Badge

![Health badge](/badges/kolirt-laravel-master-model/health.svg)

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

###  Alternatives

[laravel/laravel

The skeleton application for the Laravel framework.

84.5k62.4M1.0k](/packages/laravel-laravel)[unopim/unopim

UnoPim Laravel PIM

10.3k2.2k](/packages/unopim-unopim)[nasirkhan/laravel-starter

A CMS like modular Laravel starter project.

1.4k2.7k](/packages/nasirkhan-laravel-starter)[codewithdennis/larament

Larament is a time-saving starter kit to quickly launch Laravel 13.x projects. It includes FilamentPHP 5.x pre-installed and configured, along with additional tools and features to streamline your development workflow.

3861.7k](/packages/codewithdennis-larament)[kompo/kompo

Laravel &amp; Vue.js FullStack Components for Rapid Application Development

11914.4k42](/packages/kompo-kompo)[ercogx/laravel-filament-starter-kit

This is a Filament v5 Starter Kit for Laravel 13, designed to accelerate the development of Filament-powered applications.

461.7k](/packages/ercogx-laravel-filament-starter-kit)

PHPackages © 2026

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