First, the Database migration
Laravel's Database migration provides a series of related operations on databases, tables, fields, and indexes.
1. Create a migration
Use the Artisan command php Artisan make:migration create_links_table
This will generate a friendly link migration class named 2017_05_06_151645_create_links_table.php in the Database/migrations directory. Where the first half of the name "2017_05_06_151645_" is the timestamp of the Laravel increment.
2. Writing logic
Write code to manipulate the data table for the UP () and down () methods of the migration class.
2017_05_06_151645_create_links_table.php File Contents:
<?PHP UseIlluminate\database\schema\blueprint; Useilluminate\database\migrations\migration;classCreatelinkstableextendsmigration{/** * Perform migration * * @return void*/ Public functionUp () {Schema:: Create (' Links ',function(Blueprint$table){ $table->engine = ' MyISAM '; $table->increments (' id '); $table-string(' name ')default(')->comment (' name ')); $table-string(' title ')default(')->comment (' title ')); $table-string(' URL ')default(')->comment (' address ')); $table-integer(' sort ')default(->comment) (' sort ')); }); } /** * Rollback Migration * * @return void*/ Public functionDown () {Schema::d rop (' links '); }}2017_05_06_151645_create_links_table. php
3. Performing the migration
Use the Artisan command PHP Artisan migrate
Now, a hd_links table is created in the database and a table hd_migrations ("Hd_" is the configured table prefix) that records the migration:
Note: If you manually delete the migrated class file, use the composer Dump-autoload command to optimize the auto-load to re-make:migration.
Second, data filling
Can be used for testing to populate tables in a database with some data.
1. Create a fill
Use the Artisan command php Artisan make:seeder linkstableseeder
This will generate a friendly link fill class named linkstableseeder.php in the Database/seeds directory.
2. Writing logic
linkstableseeder.php File Contents:
<?PHP UseIlluminate\database\seeder;classLinkstableseederextendsseeder{/** * Run Database population * * @return void*/ Public functionrun () {$data= [ [ ' Name ' = ' Laravel Chinese Community ', ' title ' + ' Laravel China Community-high quality Laravel and PHP developer community-Powered by Phphub ', ' url ' = ' https://laravel-china.org/', ' sort ' = ' 49 ' ], [ ' Name ' = ' GitHub ', ' title ' = ' GitHub ' is where people build software. More than million people use ... ', ' url ' = ' https://github.com ', ' sort ' = ' 49 ' ] ]; DB:: Table (' Links ')->insert ($data); }}
3. Call the Fill
In the Database/seeds directory databaseseeder.php This database population class, the fill is called within the Run () method.
databaseseeder.php File Contents:
<? PHP Use Illuminate\database\seeder; class extends seeder{ /* * * Run database population * * @return void */ publicfunction run () { $this->call (linkstableseeder:: Class);} }
4. Perform the Fill
Use the Artisan command PHP Artisan db:seed
Now, the Hd_links table in the database has 2 records:
Laravel 5.2 Database migration and data population