Home >PHP Framework >Laravel >Five steps to use Laravel Migrations

Five steps to use Laravel Migrations

藏色散人
藏色散人forward
2021-09-01 16:10:592599browse

This article is introduced to you by the Laravel tutorial column. The main content is "How to use Migrations in Laravel". I hope it will be helpful to friends in need!

Laravel: Using Migrations

1. First use artisan to create a migratable data table template. After running this command, it will be in the database/migrations directory. Generate a file

php artisan make:migration create_fees_count_table --create=fees_count

2. The generated file contains two methods, up and down. Up contains descriptions of adding tables, adding columns, adding indexes, etc., and down is relatively simple, which is to delete tables. , of course there can be some other logic in it

3. The data table column types supported in up. Make a note and will not translate it for the time being

##$table->bigIncrements('id');Incrementing ID (primary key) using a "UNSIGNED BIG INTEGER" equivalent.$table->bigInteger('votes');BIGINT equivalent for the database.$table->binary('data');BLOB equivalent for the database.##$table->boolean(' confirmed');##$table->char('name', 4);CHAR equivalent with a length.$table->date('created_at');DATE equivalent for the database.$table->dateTime('created_at');DATETIME equivalent for the database.DECIMAL equivalent with a precision and scale.DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point.ENUM equivalent for the database.FLOAT equivalent for the database. Incrementing ID (primary key) using a "UNSIGNED INTEGER" equivalent.INTEGER equivalent for the database.##$table->json(' options');JSON equivalent for the database.$table->jsonb('options');JSONB equivalent for the database.$table->longText('description');LONGTEXT equivalent for the database. $table->mediumInteger('numbers');MEDIUMINT equivalent for the database.##$table->morphs('taggable');Adds INTEGER taggable_id and STRING taggable_type.$table->nullableTimestamps();Same as timestamps(), except allows NULLs.$table-> ;rememberToken();Adds remember_token as VARCHAR(100) NULL.$table->smallInteger('votes'); SMALLINT equivalent for the database.$table->softDeletes();Adds deleted_at column for soft deletes.$table->string('email');VARCHAR equivalent column.$table->string('name', 100); VARCHAR equivalent with a length.$table->text('description');TEXT equivalent for the database.$table->time('sunrise');TIME equivalent for the database.$table-> ;tinyInteger('numbers');TINYINT equivalent for the database.$table->timestamp('added_on'); TIMESTAMP equivalent for the database.$table->timestamps();Adds created_at and updated_at columns.$table->uuid('id');UUID equivalent for the database.
php artisan  migrate
Command Description
BOOLEAN equivalent for the database.
##$table->decimal('amount', 5 , 2);
$table->double('column', 15, 8);
$table->enum('choices', ['foo', 'bar'] );
$table->float('amount');
$table->increments('id');
$table->integer('votes');
##$table->mediumText('description'); MEDIUMTEXT equivalent for the database.
4. After the table is created, execute it directly. Since I did not use php artisan migrate before creating many tables, running this command directly resulted in a prompt that some tables existed. Therefore, I transferred this file to the tmp directory under the database and added --path 'database/tmp' to the command. The operation is successful again 5. Observe in the database and find that the table has been created!
Related recommendations:

The latest five Laravel video tutorials

The above is the detailed content of Five steps to use Laravel Migrations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete