Laravel-based migrate tutorial, laravelmigrate
Preface
As we all know, the current development and testing are completed by collaboration among multiple teams. Everyone has a local environment. In the past, we generally manually add data, for example, you can use SQL statements in the database queryserver to insert data. If there is a small amount of data, it would be quite easy, but if the data is too large, it would be very painful, but it would be very easy in Laravel, you can use data migration.
This article gives a detailed introduction to the use of migrate in Laravel. I will share the content for your reference. I will not talk about it much below. Let's take a look at the detailed introduction:
Generate migration
Command:
Migration
php artisan make:migration create_users_table
Meaning:Creating a migration involves creating a table named users.
Then you can find a file similar to 2014_10_12_000000_create_users_table.php in the database/migrations directory.
Like using a php statement to create a table, you can write the fields and constraints for creating a table in the file 2014_10_12_000000_create_users_table.php.
The-table and-create options can be used to specify the table name and whether to create a new data table for the migration. These options only need to be placed behind the preceding migration command and specify the table name. If you want to specify the custom output path for the generated migrationmake:migration
You can use the-path option when running the command. The provided path is relative to the application root directory.
Migration Structure
A migration class contains two methods: up and down.
Up mainly contains the specific content of creating a table.
Down is opposite to the former.
Schema::create
Two parameters are accepted. The first is the name of the table to be created, and the second is a closure (anonymous function) to obtain the Blueprint object used to define the new table.
Migration
<?php use Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); }}
Run migration
To run all unexecuted migration tasks in the application, you can use the migrate method of the Artisan command.
Migration
php artisan migrate
Rollback migration
To roll back the latest migration "operation", you can use the rollback command. Note that this will roll back the last batch of running migration, which may contain multiple migration files:
Migration
php artisan migrate:rollback
The migrate: reset command will roll back all applications for Migration:
Migration
php artisan migrate:reset
Rollback/migration in a single command
migrate:refresh
The command first rolls back all databases for migration, and then runs the migrate command. This command can effectively reconstruct the entire database:
Migration
php artisan migrate:refreshphp artisan migrate:refresh --seed
Common migration attributes
$ Table-> increments ('id '); |
Database primary key auto-increment ID |
$ Table-> integer ('votes '); |
Equivalent to the INTEGER type in the database |
$ Table-> float ('amount '); |
Equivalent to the FLOAT type in the database |
$ Table-> char ('name', 4 ); |
Equivalent to the CHAR type in the database |
$ Table-> dateTime ('created _ '); |
Equivalent to the DATETIME type in the database |
$ Table-> enum ('choices', ['foo', 'bar']); |
Equivalent to the ENUM type in the database |
$ Table-> tinyInteger ('numbers '); |
Equivalent to the TINYINT type in the database |
$ Table-> timestamps (); |
Add created_at and updated_at Columns |
Writing constraints for some column names
Migration
Schema: table ('users', function ($ table) {$ table-> integer ('votes ')-> unsigned (); // unsigned type });
Common constraints
-> First () |
Set this column as the first column in the table (for MySQL only) |
-> After ('column ') |
Place this column after another column (For MySQL only) |
-> Nullable () |
The value of this column is allowed to be NULL. |
-> Default ($ value) |
Default Value of the specified Column |
-> Unsigned () |
Set integer columns to UNSIGNED |
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.