Рубрики
Uncategorized

Наполнитель базы данных (поставляется laravel)

Автор оригинала: David Wong.

//Write filler, CMD goes to project root
PHP artican make: seeder userstableseeder // all frame generated fillers are in database / seeds
//The stuffer class by default contains only one method: run. This method is called when the artisan command DB: seed runs.

//Test add single
php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
    /**
     *Run database population
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => str_random(10),
            'email' => str_random(10).'@gmail.com',
            'password' => bcrypt('secret'),
        ]);
    }
}
//Or add a usage model factory in the userstabseeder test 
    public function run()
    {
        $users = Factory (\ app \ models \ users:: class, 50000) - > make(); // create users model in app's models
        \App\Models\Users::insert( $users->toArray() );
    }
    // in the DatabaseSeeder class
    public function run()
    {
        $this->call([
             UsersTableSeeder::class,
        ]);
    }
}
//After writing the stuffer class, you need to regenerate the automatic loader of composer through the dump autoload command:
composer dump-autoload
//You can use the artisan command DB: seed to populate the database.
php artisan db:seed