Skip to content

Fill the database with required data

License

Notifications You must be signed in to change notification settings

Kerigard/laravel-data

Repository files navigation

Fill the database with required data

Build Status Total Downloads Latest Stable Version License

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.