PHP code example of reedware / laravel-seeders

1. Go to this page and download the library: Download reedware/laravel-seeders library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reedware / laravel-seeders example snippets


'providers' => [
    /* ... */
    Reedware\LaravelSeeders\SeederServiceProvider::class
    /* ... */
]


'aliases' => [
    'Seed' => Reedware\LaravelSeeders\Seed::class
]


app('db.seed')
// or
app(Reedware\LaravelSeeders\Contracts\Factory::class)




use Reedware\LaravelSeeders\ResourceSeeder;

class PermissionSeeder extends ResourceSeeder
{
    /**
     * The model this resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Permission::class;

    /**
     * The attributes to match when creating or updating records.
     *
     * @var array
     */
    public static $match = ['name'];

    /**
     * The columns to used order the resources in storage.
     *
     * @var array
     */
    public static $orderBy = ['name' => 'asc'];
}




use Reedware\LaravelSeeders\ResourceSeeder;

class MySeeder extends ResourceSeeder
{
    /* ... */

    /**
     * Whether or not new records in storage can be inserted into the database.
     *
     * @var boolean
     */
    public static $allowCreating = true;

    /**
     * Whether or not existing records in storage can be updated within the database.
     *
     * @var boolean
     */
    public static $allowUpdating = true;

    /**
     * Whether or not missing records in storage can be deleted from the database.
     *
     * @var boolean
     */
    public static $allowDeleting = true;
}



use Reedware\LaravelSeeders\ResourceSeeder;

class MySeeder extends ResourceSeeder
{
    /* ... */

    /**
     * Whether or not to omit the primary key.
     *
     * @var boolean
     */
    public static $omitPrimaryKey = false;
}



use Reedware\LaravelSeeders\ResourceSeeder;

class MySeeder extends ResourceSeeder
{
    /* ... */

    /**
     * Maps the model attributes to stored attributes.
     *
     * @param  array  $attributes
     *
     * @return array
     */
    public static function mapAttributes(array $attributes)
    {
        $attributes = parent::mapAttributes($attributes);

        $parts = explode(' ', $attributes['full_name']);

        $attributes['first_name'] = array_shift($parts);
        $attributes['last_name'] = $parts;

        return $attributes;
    }

    /**
     * Unmaps stored attributes to model attributes.
     *
     * @param  array  $attributes
     *
     * @return array
     */
    public static function unmapAttributes(array $attributes)
    {
        $attributes = parent::unmapAttributes($attributes);

        $attributes['full_name'] = $attributes['first_name'] . ' ' . $attributes['last_name'];
    }
}



use Reedware\LaravelSeeders\ResourceSeeder;

class MySeeder extends ResourceSeeder
{
    /* ... */

    /**
     * Additional columns to omit when seeding.
     *
     * @var array
     */
    public static $omit = [
        'pretend'
    ];
}



use Reedware\LaravelSeeders\ResourceSeeder;

class MySeeder extends ResourceSeeder
{
    /* ... */

    /**
     * Returns whether or not existing records in storage can be updated within the database.
     *
     * @return boolean
     */
    public static function allowUpdating()
    {
        return app()->environment() !== 'production';
    }
}



use Illuminate\Support\ServiceProvider;
use Seed;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Seed::setRootPath('where/ever/you/want');
    }
}

Seed::filename(MyModel::class, 'my-filename.csv');


Seed::guessFilenamesUsing(function($class) {
    return $class . '.csv';
});




use Reedware\LaravelSeeders\Contracts\Reader;

class MyReader implements Reader
{
    //
}

Seed::readUsing(function($filename) {
    return new MyReader($filename);
});




use Reedware\LaravelSeeders\Contracts\Writer;

class MyWriter implements Writer
{
    //
}

Seed::writeUsing(function($filename) {
    return new MyWriter($filename);
});