PHP code example of codingphase / seeder

1. Go to this page and download the library: Download codingphase/seeder 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/ */

    

codingphase / seeder example snippets


  'providers' => [
      // ...
      CodingPhase\Seeder\SeederServiceProvider::class,
      // ...
  ],

php artisan vendor:publish  --provider="CodingPhase\Seeder\SeederServiceProvider"

use CodingPhase\Seeder\ModelSeeder;

class UsersTableSeeder extends ModelSeeder
{
    ...
}

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run
{
    //Example
    $users = $this->seedModel(\App\User::class, function ($user) {
        $user->save();
    });
}

$this->setAmount(30)->seedModel(\App\User::class, function ($user) {
    $user->save();
});

$this->setHeader("Awesome Users")->seedModel(\App\User::class, function ($user) {
    $user->save();
});

$this->setAmount(30)->seedModel(\App\User::class, function ($user) {
    $user->save();
});

$data = [    
    1 => [
        'name' => 'test',
        'email' => '[email protected]'
    ],    
    25 => [
        'name' => 'example'
        'email' => '[email protected]',
    ],
];

$this->useData($data)->seedModel(\App\User::class, function ($user) {
    $user->save();
});

use CodingPhase\Seeder\ModelSeeder;

class UsersTableSeeder extends ModelSeeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run
    {
        $adminsData = [
            1 => [
                'name' => 'test',
                'email' => '[email protected]',
                'password' => bcrypt('123456')
            ],
            4 => [
                'name' => 'test4',
                'email' => '[email protected]',
                'password' => bcrypt('654321')
            ],
        ];
        
        $admins = $this->useData($adminsData)
            ->setAmount(5)
            ->setHeader("Seeding Admins")
            ->setCompact(false)
            ->seedModel(\App\User::class, function ($user) {
                $user->admin = 1;
                $user->save();
            });
        
        $users = $this->setHeader("Seeding Regular Users")
            ->seedModel(\App\User::class, function ($user) {
                $user->save();
            });
    }
}