An opinioned approach to extend the laravel seed classes.

Overview

Laravel Seed Extender

A highly opinioned way to work with the laravel seeder.

Installation

Require the package using composer:

composer require touhidurabir/laravel-seed-extender

WHY ?

As mentioned, this is a highly opinioned way to work with seeder. We have popular Faker library and Model Factories to seed model table and that seems like the obious choice . But sometimes in production or even in the development purpose we need real life data to seed model table and for that purpose need seeder classes .

Now this package does not introduce any new mechanism of the seeding using the seeder classes but just add some ability to manupulate the each seeder class as per need . Basically it add some extar feature/ability to the seeder class.

Usage

To generate a new seeder class of this package, run the following command

php artisan make:extend-seeder SeederClassName --table=table_name

That will generate an seeder new seeder class at the /database/seeders location . for example , a basic user seeder class will look like this

data as $key => $value) { $this->data[$key] = array_merge($value, [ ]); } return $this->data; } } ">


namespace Database\Seeders;

use Touhidurabir\SeedExtender\BaseTableSeeder;

class UsersTableSeeder extends BaseTableSeeder {
    
    /**
     * Seeder table name 
     *
     * @var string
     */
    protected $table = "users";


    /**
     * The list of table attributes/columns
     *
     * @var array
     */
    protected $columns = ["id", "email", "password", "created_at", "updated_at", "deleted_at"];


    /**
     * The table attributes/columns that will be ignored during the seeding process
     *
     * @var array
     */
    protected $ignorables = [];


    /**
     * The table attributes/columns that will be used during the seeding process
     *
     * @var array
     */
    protected $useables = [];


    /**
     * Should merge and include timestamps[created_at, updated_at] by default into the seed data
     *
     * @var boolean
     */    
    protected $includeTimestampsOnSeeding = true;


    /**
     * The seeding data
     *
     * @var array
     */
    protected $data = [
    	
    ];


    /**
     * Build up the seedeable data set;
     *
     * @return array
     */
    protected function seedableDataBuilder() {

        foreach ($this->data as $key => $value) {
            
            $this->data[$key] = array_merge($value, [

            ]);
        }

        return $this->data;
    }
}

As the above example showes, there are few properties that can be modified manually or vai the command.

Class properties and methods explanation

$table (PROPERTY)

It define for which talbel it will run the seeder . to specify which table vai the command

php artisan make:extend-seeder UsersTableSeeder --table=users

$columns (PROPERTY)

It defined all the available columns in the table . nothing to do here as it will be auto generated by checking the table schema by this package itself .

$ignorables (PROPERTY)

It defined which columns will be ignored at the seed time . it specify which columns we want to ignore at the class generation time through the command, provide comma seperated columns name

php artisan make:extend-seeder UsersTableSeeder --table=users --ignorables=id,deleted_at

$useables (PROPERTY)

It defined which columns will be used at the seed time . it specify which columns we want to use at the class generation time through the command, provide comma seperated columns name

php artisan make:extend-seeder UsersTableSeeder --table=users --useables=name,email,password

NOTE that if the $useables property and defined and not empty, it will take account of it and ignore the set values of $ignorables property.

$includeTimestampsOnSeeding (PROPERTY)

This defined if the created_at and updated_at values will be auto included at the seed time. by default this is set to true . but if the model is not using the timestamp values or do not want to include it in the seding process, then specify it through the command at the time of generation

php artisan make:extend-seeder UsersTableSeeder --table=users --useables=name,email,password --no-timestamp

$data (PROPERTY)

This defined what data to seed . for example

protected $data = [
    ['[email protected]', '123456'],
    ['[email protected]', '123456'],
];

seedableDataBuilder (METHOD)

If we have some data that needed to be presetent to every seeding rows, then it's better to not to have the as repetitive data in the $data properties and define those in this method .

protected function seedableDataBuilder() {

    foreach ($this->data as $key => $value) {
        
        $this->data[$key] = array_merge($value, [
            // any repetitive merge data 
            // it will merge to every row data defined in the $data proeprties
        ]);
    }

    return $this->data;
}

More Command Options

replace

By default it will throw exception and print a error message in the console when a seeder class of same name already exists but if needed to replace it pass the flag --replace

php artisan make:extend-seeder UsersTableSeeder --table=users --replace

strict

By default this package will not try to validate the give informaitons like table name it useables or ignorables columns but if required to , it can validate those via passing the flag --strict .

php artisan make:extend-seeder UsersTableSeeder --table=users --useables=email,password --strict

Run seeding independent of seeder class

This package provides way to run a seeding process independent of seeder class . That is one can run a seeding process from within the app at the runtime .

use Touhidurabir\SeedExtender\Facades\SeedExtender;

SeedExtender::table('table_name') //table name
    ->useables([]) // useables columns as array
    ->ignorables([]) // ignorables columns as array
    ->includeTimestampsOnSeeding(true) // auto include of timestamp value as boolean
    ->seedData( // set seed data
        [ [], [], ], // the seed data itself
        [] // any auto mergeable repetitive data
    )
    ->run(); // run the seeding process

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

You might also like...
Collection of classes you can use to standardize data formats in your Laravel application.
Collection of classes you can use to standardize data formats in your Laravel application.

Laravel Formatters This package is a collection of classes you can use to standardize data formats in your Laravel application. It uses the Service Co

Simple transactional email classes/templates for Laravel 5 mailables
Simple transactional email classes/templates for Laravel 5 mailables

Tuxedo Tuxedo is an easy way to send transactional emails with Laravel's Mail classes, with the templates already done for you. Contents Installation

A collection of classes to be extended/used in laravel apps for quick development

laraquick A collection of classes to be extended/used in laravel applications for quick development. Introduction The library contains traits with wel

🏭This package lets you create factory classes for your Laravel project.
🏭This package lets you create factory classes for your Laravel project.

Laravel Factories Reloaded 🏭 This package generates class-based model factories, which you can use instead of the ones provided by Laravel. Laravel 8

Package with small support traits and classes for the Laravel Eloquent models

Contains a set of traits for the eloquent model. In future can contain more set of classes/traits for the eloquent database.

A simple artisanal command framework for creating service layer classes

Introdução Este projeto tem como objetivo fornecer alguns comandos adicionais à interface de linha de comando do Laravel para manipular a estrutura da

States allows you to create PHP classes following the State Pattern in PHP.

States allows you to create PHP classes following the State Pattern in PHP. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and this improve maintainability and workflows writing.

PHP components - collection of cross-project PHP classes

PHP components Collection of cross-project PHP classes. Install: $ composer require ansas/php-component Ansas\Component\Convert\ConvertPrice Convert "

A university system that creates a timetable programm for subjects,classes and teachers which is used to create a programm for each semester. All this served as a website.

Timetable-System-Generator A university system that creates a timetable programm for subjects,classes and teachers which is used to create a programm

Releases(1.1.1)
Owner
Touhidur Rahman
Husband, father, big time documentary tv series lover and software engineer . Passionate about PHP, Laravel, Ruby on Rails, Vue.js and C/C++ . Learning Rust .
Touhidur Rahman
Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Octohook 13 May 16, 2022
Extend Laravel PHP framework to make working with Aiven databases simpler

Aiven Commands for Laravel ✨ Add some Aiven magic to your Laravel project ✨ This Laravel package provides some aiven commands for artisan to help with

Aiven 8 Aug 19, 2022
This is huaweinvr extend for laravel

This is huaweinvr extend for laravel

Verus 1 Nov 18, 2021
Extend Kirby’s templates with a powerful layout system

Kirby Layouts plugin This plugin extends Kirby’s templates with a powerful layout system. Installation Download Download and copy this repository to /

Kirby 3 39 Dec 28, 2022
A simple and modern approach to stream filtering in PHP

clue/stream-filter A simple and modern approach to stream filtering in PHP Table of contents Why? Support us Usage append() prepend() fun() remove() I

Christian Lück 1.5k Dec 29, 2022
A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Adnane Kadri 49 Jun 22, 2022
Load files and classes as lazy collections in Laravel.

Lody Load files and classes as lazy collections in Laravel. Installation composer require lorisleiva/lody Usage Lody enables you to fetch all exist

Loris Leiva 64 Dec 22, 2022
Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

Egor Shitikov 87 Nov 26, 2022
A laravel package to attach uuid to model classes

Laravel Model UUID A simple package to generate model uuid for laravel models Installation Require the package using composer: composer require touhid

null 10 Jan 20, 2022
Use Laravel's built-in ORM classes to query cloud resources with Steampipe.

Laravel Steampipe Use Laravel's built-in ORM classes to query cloud resources with Steampipe, an open source CLI to instantly query cloud APIs using S

Renoki Co. 13 Nov 8, 2022