On the analysis of Laravel Foundation migrations

Source: Internet
Author: User

I. Migration creating data tables and populating data with Seeder databases

Database Migrations are like 版本控制 databases that make it easy for your team to modify and share the database structure
of your application

1.1 Creating a migration

PHP artisan make:migration create_users_table--create=usersphp artisan make:migration add_votes_to_users_table-- Table=users//Add Field

The new migration file will be placed in the database/migrations directory. The name of each migration file contains a timestamp so that you can Laravel confirm the order of the migration.
--tableand --create options can be used to specify the name of the data table, or whether the new data table will be created when the migration is executed.

1.2 Migration structure

A migration class typically contains two methods: up and down . upmethod to add a new data table, field, or index to the database, and the method down is up the inverse of the method. You can use the Laravel database structure Builder in both methods to create and modify data tables.

1.2.1 Creating a data table

/**     * Run Database Migration     *     * @return void */public    function up ()    {        schema::create (' flights ') function (Blueprint $table) {            $table->increments (' id ');            $table->string (' name ')->comment (' field annotations ');            $table->string (' Airline ')->comment (' field annotations ');            $table->timestamps ();        });    }    /**     * Rollback Database Migration     *     * @return void *     /Public Function down    ()    {        Schema::d rop (' Flights ');    }

1.2.2 Adding fields to a table

data tables, fields, indexes:https://laravel-china.org/doc ...

1.3 Running the migration

To run all outstanding migrations:php artisan migrate

1.4 Rollback migration

To roll back the last migration, you can use the rollback command:

PHP artisan migrate:rollbackphp Artisan migrate:rollback--step=5//Rollback Migration number PHP artisan Migrate:reset//Rollback all migrated PHP in the application Artisan Migrate:refresh//Command not only rolls back all migrations of the database but also runs the migrate command PHP artisan migrate  //recovery

1.5 Populating a database with data using the Seeder method

1.5.1 Writing Seeders

PHP Artisan Make:seeder Userstableseeder

1.5.2 Database population

/**     * Run database population     *     * @return void     *    /Public Function run ()    {        db::table (' users ')->insert ([            ' name ' = + str_random,            ' email ' = ' str_random (10) '. @gmail. com ',            ' password ' = bcrypt (' secret '),        ]);    

use model factory classes to create test data in batches

PHP artisan make:factory postfactory-m Post//-M indicates the model of the binding

1.5.3 Calling other Seeders

In a DatabaseSeeder class, you can use call methods to run other seed classes.

/** * Run the database seeds. * * @return void */public function run () {    $this->call ([        userstableseeder::class,        poststableseeder:: Class,        Commentstableseeder::class,    ]);}

1.5.4 Running Seeders

By default, the db:seed command runs the DatabaseSeeder class, which can be used to invoke other Seed classes. However, you can also use --class the option to specify a specific seeder class:

PHP artisan db:seedphp Artisan db:seed--class=userstableseeder

You can also migrate:refresh populate the database with a command that rolls back and rerun all migrations. This command can be used to rebuild the database:

PHP Artisan Migrate:refresh--seed

Second, the model

To create a model:

PHP artisan make:model models/goodsphp artisan make:model models/goods-m  //Generate corresponding migration file at the same time

Third, routing

To create a route in bulk: (Resource routing)

PHP artisan make:controller usercontroller--resourceroute::resource (' user ', ' usercontroller '); Batch definition of ' 7 ' routes at once

get details based on unique field values for SEO

Laravel 5.5 Nginx configuration:
Root/example.com/public;
Location/{

    Try_files $uri $uri//index.php? $query _string;}

Location =/favicon.ico {access_log off; log_not_found off;}
Location =/robots.txt {access_log off; log_not_found off;}

Iv. Verification

4.1 Quick Verification

4.2 Form Request Validation

PHP Artisan make:request Storeblogpost

Difference and attention

1. Find and get

Find: Returns the specified data by primary key

$result = Student::find (1001);

Get-Query multiple data results

Db::table ("table name")->get ();D b::table ("table name")->where (condition)->get ();

2. Binding the model to the data table

Create model type, declare two protected properties in method: $table (table name) and $primarykey (primary key)

<?phpnamespace App;   Use Illuminate\database\eloquent\model;class Student extends model{    protected $table = ' Student ';    Protected $primaryKey = ' id ';}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.