{"id":22013173,"url":"https://github.com/kolirt/laravel-master-model","last_synced_at":"2025-05-06T21:43:54.509Z","repository":{"id":57008168,"uuid":"169800732","full_name":"kolirt/laravel-master-model","owner":"kolirt","description":"🧖‍♂️ Package that simplifies working with models, saving relationships, and uploading files in Laravel","archived":false,"fork":false,"pushed_at":"2024-09-11T09:41:02.000Z","size":19,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-11T15:38:08.036Z","etag":null,"topics":["laravel-file-manager","laravel-file-upload","laravel-model","laravel-package","laravel-relationship"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kolirt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":"https://www.buymeacoffee.com/kolirt"}},"created_at":"2019-02-08T21:22:24.000Z","updated_at":"2024-09-11T09:39:37.000Z","dependencies_parsed_at":"2024-09-11T11:59:37.896Z","dependency_job_id":"fa2a14c3-7a2d-47d6-941e-c80eaf438f1d","html_url":"https://github.com/kolirt/laravel-master-model","commit_stats":{"total_commits":29,"total_committers":1,"mean_commits":29.0,"dds":0.0,"last_synced_commit":"8848696814920b7c53974c17bc45ca054d2c62b7"},"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kolirt%2Flaravel-master-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kolirt%2Flaravel-master-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kolirt%2Flaravel-master-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kolirt%2Flaravel-master-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kolirt","download_url":"https://codeload.github.com/kolirt/laravel-master-model/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252776251,"owners_count":21802459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel-file-manager","laravel-file-upload","laravel-model","laravel-package","laravel-relationship"],"created_at":"2024-11-30T03:17:52.411Z","updated_at":"2025-05-06T21:43:54.474Z","avatar_url":"https://github.com/kolirt.png","language":"PHP","funding_links":["https://www.buymeacoffee.com/kolirt"],"categories":[],"sub_categories":[],"readme":"# Laravel Master Model\nLaravel Master Model is a powerful package for the Laravel framework that simplifies working with models, particularly\nin **saving relations** and **uploading files**.\n\nThis package is designed for developers who want to optimize the process of working with databases and files, reducing\ncode complexity and enhancing performance.\n\n\n## Structure\n- [Getting started](#getting-started)\n  - [Requirements](#requirements)\n  - [Installation](#installation)\n  - [Setup](#setup)\n- [Console commands](#console-commands)\n- [Use cases](#use-cases)\n  - [Saving files](#saving-files)\n  - [Saving files from third-party resources](#saving-files-from-third-party-resources)\n  - [Deleting files](#deleting-files)\n  - [Saving `HasOne`, `MorphOne` relations](#saving-hasone-morphone-relations)\n  - [Saving `HasMany`, `MorphMany` relations](#saving-hasmany-morphmany-relations)\n  - [Saving `HasMany`, `MorphMany` relations with `sync` mode](#saving-hasmany-morphmany-relations-with-sync-mode)\n  - [Saving `BelongsToMany` relation](#saving-belongstomany-relation)\n  - [Saving `BelongsToMany` relation with `sync` mode](#saving-belongstomany-relation-with-sync-mode)\n  - [Response file](#response-file)\n- [FAQ](#faq)\n- [License](#license)\n- [Other packages](#other-packages)\n\n\u003ca href=\"https://www.buymeacoffee.com/kolirt\" target=\"_blank\"\u003e\n  \u003cimg src=\"https://cdn.buymeacoffee.com/buttons/v2/arial-yellow.png\" alt=\"Buy Me A Coffee\" style=\"height: 60px !important;width: 217px !important;\" \u003e\n\u003c/a\u003e\n\n\n## Getting started\n\n### Requirements\n- PHP \u003e= 8.1\n- Laravel \u003e= 10\n\n\n### Installation\n```bash\ncomposer require kolirt/laravel-master-model\n```\n\n\n### Setup\nPublish config file\n\n```bash\nphp artisan master-model:install\n```\n\nUse the `MasterModel` trait in your models\n\n```php\nuse Kolirt\\MasterModel\\MasterModel;\n\nclass Item extends Model\n{\n    use MasterModel;\n}\n```\n\n\n## Console commands\n- `master-model:install` - Install master model package\n- `master-model:publish-config` - Publish the config file\n\n\n## Use cases\n\n### Saving files\n```php\nclass Item extends Model\n{\n    use MasterModel;\n\n    protected $fillable = [\n        'image',\n    ];\n}\n```\n\nMasterModel automatically saves the file and deletes the old file, if it existed\n\n```php\n\nclass ExampleController extends Controller\n{\n    public function index(Request $request, $id)\n    {\n        $data = $request-\u003evalidate([\n            'image' =\u003e 'required|file',\n        ]);\n\n        $item = Item::query()-\u003efindOrFail($id);\n        $item-\u003eupdate($data);\n    }\n}\n```\n\nYou can specify folder and disk for each file\n\n```php\nclass Item extends Model\n{\n    use MasterModel;\n\n    protected $fillable = [\n        'image',\n    ];\n    \n    protected string $upload_model_folder = 'items';\n\n    protected array $upload_folders = [\n        'image' =\u003e 'image',\n    ];\n\n    protected array $upload_disks = [\n        'image' =\u003e 'public'\n    ];\n}\n```\n\n\n### Saving files from third-party resources\nYou no longer need to worry about saving files from third-party resources, just put the `response` and MasterModel will save everything for you\n\n```php\nclass ExampleController extends Controller\n{\n    public function index($id)\n    {\n        $file1_url = 'https://png.pngtree.com/png-clipart/20230126/original/pngtree-fresh-red-apple-png-image_8930987.png';\n        $response = \\Illuminate\\Support\\Facades\\Http::get($file1_url);\n\n        $file2_url = 'https://cubanvr.com/wp-content/uploads/2023/07/ai-image-generators.webp';\n        $client = new \\GuzzleHttp\\Client();\n        $response2 = $client-\u003eget($file2_url);\n\n        Item::create([\n            'image' =\u003e $response,\n            'image2' =\u003e $response2\n        ]);\n    }\n}\n```\n\n\n### Deleting files\nYou can delete files by setting the field to `null`\n\n```php\n$item = Item::query()-\u003efirst();\n\n$item-\u003eupdate([\n    'image' =\u003e null\n]);\n```\n\nTo 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\n\n_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_\n\n```php\n$item = Item::query()-\u003ewith(['phone', 'addresses'])-\u003efirst();\n/**\n* All files in the model and in the loaded relations will be deleted\n */\n$item-\u003edelete();\n```\n\n\n### Saving `HasOne`, `MorphOne` relations\nYou can **save** `HasOne`, `MorphOne` relations in the same way as a file. If relation exists, it will be updated, otherwise it will be created\n\n```php\n$item = Item::query()-\u003efirst();\n\n$item-\u003eupdate([\n    'phone' =\u003e [ // hasOne, morphOne relation\n        'number' =\u003e '1234567890'\n    ]\n]);\n```\n\nYou can also **delete** the relation by setting it to `null`\n\n```php\n$item = Item::query()-\u003efirst();\n\n$item-\u003eupdate([\n    'phone' =\u003e null // hasOne, morphOne relation\n]);\n```\n\n\n### Saving `HasMany`, `MorphMany`  relations\nYou can **save** `HasMany`, `MorphMany` relations in the same way as a file. If relations exists, it will be updated, otherwise it will be created\n\n```php\n$item = Item::query()-\u003efirst();\n\n$item-\u003eupdate([\n    'phones' =\u003e [ // hasMany, morphMany relations\n        [ // will be created\n            'number' =\u003e '1234567890'\n        ],\n        [ // will be updated (id = 1)\n            'id' =\u003e 1,\n            'number' =\u003e '0987654321'\n        ]\n    ]\n]);\n```\n\n\n### Saving `HasMany`, `MorphMany` relations with `sync` mode\nYou can also **sync** `HasMany`, `MorphMany` relations. Unspecified relations will be deleted\n\n```php\n$item = Item::query()-\u003efirst();\n\n$item-\u003eupdate([\n    'phones' =\u003e [ // hasMany, morphMany relations\n        'mode' =\u003e 'sync', // not specified relations will be deleted\n        'value' =\u003e [\n            [ // will be created\n                'number' =\u003e '1234567890'\n            ],\n            [ // will be updated (id = 1)\n                'id' =\u003e 1,\n                'number' =\u003e '0987654321'\n            ]\n        ]\n    ]\n]);\n```\n\n\n### Saving `BelongsToMany` relation\n\n```php\n$item = Item::query()-\u003efirst();\n\n$item-\u003eupdate([\n    'categories' =\u003e [1, 2, 3] // belongsToMany relations\n]);\n\n$item-\u003eupdate([\n    'categories' =\u003e [ // belongsToMany relation\n        1 =\u003e ['name' =\u003e 'Category 1'], \n        2 =\u003e ['name' =\u003e 'Category 2'], \n        3 =\u003e ['name' =\u003e 'Category 3']\n    ]\n]);\n```\n\n\n### Saving `BelongsToMany` relation with `sync` mode\nYou can **sync** the `BelongsToMany` relation. Everything that is not specified when saving will be deleted\n\n```php\n$item = Item::query()-\u003efirst();\n\n$item-\u003eupdate([\n    'categories' =\u003e [ // belongsToMany relation\n        'mode' =\u003e 'sync', // not specified relations will be deleted\n        'value' =\u003e [1, 2, 3]\n    ]\n]);\n\n$item-\u003eupdate([\n    'categories' =\u003e [ // belongsToMany relation\n        'mode' =\u003e 'sync', // not specified relations will be deleted\n        'value' =\u003e [\n            1 =\u003e ['name' =\u003e 'Category 1'], \n            2 =\u003e ['name' =\u003e 'Category 2'], \n            3 =\u003e ['name' =\u003e 'Category 3']\n        ]\n    ]\n]);\n```\n\n\n### Response file\nUse the `responseFile` method to return a file in a controller\n\n```php\nclass FileController extends Controller\n{\n\n    public function index()\n    {\n        $item = Item::query()-\u003efirst();\n        return $item-\u003eresponseFile('image');\n    }\n\n}\n```\n\n\n## FAQ\nCheck closed [issues](https://github.com/kolirt/laravel-master-model/issues) to get answers for most asked questions\n\n\n## License\n[MIT](LICENSE.txt)\n\n\n## Other packages\nCheck out my other packages on my [GitHub profile](https://github.com/kolirt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkolirt%2Flaravel-master-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkolirt%2Flaravel-master-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkolirt%2Flaravel-master-model/lists"}