Birmingham mumbai


Download 1.3 Mb.
Pdf ko'rish
bet65/68
Sana05.01.2022
Hajmi1.3 Mb.
#227543
1   ...   60   61   62   63   64   65   66   67   68
Bog'liq
Laravel 5 Essentials

$ 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.

Seeding the database

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()

 

method to load data from say, a CSV or JSON file. There are also third-party libraries 



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');




Your First Application


Download 1.3 Mb.

Do'stlaringiz bilan baham:
1   ...   60   61   62   63   64   65   66   67   68




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling