amestsantim/laravel-seeder-with-data-generator

This package is abandoned and no longer maintained. No replacement package was suggested.

Generate laravel seeders with data

1.1 2019-05-29 06:57 UTC

This package is auto-updated.

Last update: 2023-05-12 07:02:45 UTC


README

This is a laravel package which adds a generator command (make) to your artisan commands that will generate a database seeder class for you. It is similar to the included seeder class generator except that this generator allows you to pass data to it to include in the seeder class.

This can come in handy in situations where you might have database entries you want to be included with your deployment (such as roles and permissions, etc.)

make:seeder-with-data

Install

composer require amestsantim/laravel-seeder-with-data-generator

Usage

make:seeder-with-data [options] [--] <tableName> [<data>]

Arguments:
     tableName             The name of the DB table
     data                  The data, as a serialized array of named index arrays [default: "[]"]
Options:
      --path[=PATH]         Path where the seeder file should be saved
      --timestamps          If present, this switch will enable the automatic insertion of timestamps

Examples

php artisan make:seeder-with-data permissions "[['guard_name' => 'web', 'name' => 'Make Seeders'], ['guard_name' => 'web', 'name' => 'Run Seeders']]"

Notice the format that we use, the data argument is passed in as is. Remember to put it in double quotation marks.

...this will give you:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class GenericModel extends Model
{
    public $timestamps = false;
}

class PermissionsTableSeeder extends Seeder
{
    public function run()
    {
        $rowArray = [['guard_name' => 'web', 'name' => 'Make Seeders'], ['guard_name' => 'web', 'name' => 'Run Seeders']];
        $model = new GenericModel();
        $model->setTable('permissions');
        foreach ($rowArray as $row) {
            $model->create($row);
        }
    }
}

You can also specify where the generated seeder is saved by using the --path option like so,

php artisan make:seeder-with-data permissions "[['guard_name' => 'web', 'name' => 'Make Seeders'], ['guard_name' => 'web', 'name' => 'Run Seeders']]" --path="/storage/app/seeders"

It will create the directories in the path if they do not already exist.

If you omit the data argument, the default, which is an empty array will be used so that the seeder will still run without any errors but will effectively do nothing.

Specifying the --timeStamps switch will change the value of the $timestamps property on the model to true. This will enable the automatic addition of the created_at and updated_at fields in the inserted data. The command would like like this:

php artisan make:seeder-with-data permissions "[['guard_name' => 'web', 'name' => 'Make Seeders'], ['guard_name' => 'web', 'name' => 'Run Seeders']]" --timestamps