kolirt/laravel-master-model
Composer 安装命令:
composer require kolirt/laravel-master-model
包简介
The package that simplifies working with models, saving relationships, and uploading files in Laravel
README 文档
README
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
Getting started
Requirements
- PHP >= 8.1
- Laravel >= 10
Installation
composer require kolirt/laravel-master-model
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
master-model:install- Install master model packagemaster-model:publish-config- Publish the config file
Use cases
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
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
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
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
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
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
$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
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
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
Check closed issues to get answers for most asked questions
License
Other packages
Check out my other packages on my GitHub profile
kolirt/laravel-master-model 适用场景与选型建议
kolirt/laravel-master-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.27k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「upload」 「images」 「files」 「laravel」 「relations」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kolirt/laravel-master-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kolirt/laravel-master-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kolirt/laravel-master-model 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A Laravel Nova field for distribute your images as array of object.
Pexels API Client for www.pexels.com
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
Laravel Media Popup to upload/view the files
统计信息
- 总下载量: 1.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-09