The package that simplifies working with models, saving relationships, and uploading files in Laravel
kolirt/laravel-master-model is a Laravel package for the package that simplifies working with models, saving relationships, and uploading files in laravel.
It currently has 3 GitHub stars and 1.274 downloads on Packagist (latest version 4.0.10).
Install it with composer require kolirt/laravel-master-model.
Discover more Laravel packages by kolirt
or browse all Laravel packages to compare alternatives.
Last updated
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.
composer require kolirt/laravel-master-model
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;
}
master-model:install - Install master model packagemaster-model:publish-config - Publish the config fileclass 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'
];
}
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
]);
}
}
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();
HasOne, MorphOne relationsYou 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
]);
HasMany, MorphMany relationsYou 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'
]
]
]);
HasMany, MorphMany relations with sync modeYou 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'
]
]
]
]);
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']
]
]);
BelongsToMany relation with sync modeYou 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']
]
]
]);
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');
}
}
Check closed issues to get answers for most asked questions
Check out my other packages on my GitHub profile