kerigard/laravel-data
最新稳定版本:v1.0.2
Composer 安装命令:
composer require kerigard/laravel-data
包简介
Fill the database with required data
关键字:
README 文档
README
This package adds an alternative way to populate the database regarding migrations and seeders.
Installation
Install package via composer:
composer require kerigard/laravel-data
Usage
Implement the MustFillData interface into the model and set the required data.
use Illuminate\Database\Eloquent\Model; use Kerigard\LaravelData\Contracts\MustFillData; use Kerigard\LaravelData\Data; class Role extends Model implements MustFillData { public function data(): Data { return Data::make([ ['id' => 1, 'name' => 'Admin'], ['id' => 2, 'name' => 'User'], ]); } }
Run artisan command.
php artisan db:data
As a result of executing the command, all unnecessary data will be deleted from the table and new ones will be inserted if they do not already exist.
You can also disable deleting existing data from the table.
public function data(): Data { return Data::make([ // ... ], false); }
Use the withoutEvents method to disable all model events while a command is running. As a parameter, you can pass a list of events that should not be ignored.
class MyModel extends Model implements MustFillData { public static function booted() { static::creating(function (MyModel $model) { $model->slug = Str::slug($model->name); }); } public function data(): Data { return Data::make([ // ... ])->withoutEvents(['eloquent.creating: '.MyModel::class]); } }
If the model is present in the vendor package or does not exist, fill in the data in the AppServiceProvider.
use Kerigard\LaravelData\Data; use Kerigard\LaravelData\DataManager; public function boot() { DataManager::model(VendorModel::class, fn () => Data::make([ // ... ])); DataManager::table('role_user', fn () => Data::make([ // ... ])); DataManager::table([ 'connection' => 'db2', 'table' => 'permissions', 'primaryKey' => 'id', 'timestamps' => true, ], fn () => Data::make([ // ... ])); }
Changelog
Please see the CHANGELOG for more information on what has changed recently.
License
MIT. Please see the LICENSE FILE for more information.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-11