Birmingham mumbai
Download 1.3 Mb. Pdf ko'rish
|
Laravel 5 Essentials
- Bu sahifa navigatsiya:
- Seeding the database
$ php artisan migrate
When it is run for the first time, this command will also create a migrations table that Laravel will use to keep track of the migrations that have been run. It will then run any outstanding migrations. On subsequent runs, the command will use the migrations
table to determine if any migration files need running, and run them, if so. We created our breeds
table migration before the cats
table migration because we have a foreign key in our cats table. If we were to try and create the cats table first, it will fail as the column it is referencing does not exist yet.
Rather than manually populating our database, we can use the seeding helpers offered by Laravel. This time, there is no Artisan command to generate the file, but all we need to do is create a new class called BreedsTableSeeder.php at
database/seeds/ . This
class extends Laravel's Seeder
class and defines the following run()
method: class BreedsTableSeeder extends Seeder { public function run() { DB::table('breeds')->insert([ ['id' => 1, 'name' => "Domestic"], ['id' => 2, 'name' => "Persian"], ['id' => 3, 'name' => "Siamese"], ['id' => 4, 'name' => "Abyssinian"], ]); }
} You can bulk insert an array but you can also insert arbitrary code in the run()
that can help you generate large amounts of test data to fill your database, such as the excellent Faker. To control the order of execution of the seeders, Laravel lets you call them individually at database/seeds/DatabaseSeeder.php . In our case, since we only have one seeder, all we need to write is the following line: $this->call('BreedsTableSeeder'); |
ma'muriyatiga murojaat qiling