text('images')->nullable()... "That's it."/> text('images')->nullable()... "That's it.">
Home >PHP Framework >Laravel >How to change the table structure in laravel
How to change the table structure in laravel: 1. Generate migration file; 2. Execute the command "php artisan make:migration..."; 3. Add "$table->text('images')- >nullable()..." That's it.
The operating environment of this article: Windows 7 system, Laravel version 5.7, Dell G3 computer.
How to change the table structure in laravel?
Use laraval migration to modify the database table structure
I have been using sql files to save and track changes in the database table structure. However, using sql files has the following disadvantages:
It is impossible to determine which sql files have been executed and which have not. Although the date is added as the sql file name prefix, when multiple people are developing and the online data table structure has not been updated for a long time, I often have to scratch my head
Executing the sql file is laborious live. When you are faced with more than a dozen sql alter files, executing them one by one is very tiring.
Synchronizing multiple development and production environments is still time-consuming and laborious. For example, if a modification is made on the PC development machine, it must be synchronized to the laptop development environment. It is uncomfortable to think about it.
These problems are far less enjoyable than executing a line of migration.
Add a new field
For example, I want to add an images field to the articles table.
First you need to generate a migration file. Execute the command
php artisan make:migration add_images_to_articles_table --table=articles
The output result is
Created Migration: 2018_03_21_225819_add_images_to_articles_table
A file will be automatically generated in the corresponding database/migrations/ directory
database/migrations/2018_03_21_225819_add_images_to_articles_table.php
You can see that the file name is preceded by date and time.
public function up() { Schema::table('articles', function (Blueprint $table) { $table->text('images'); }); }
Follow the official documentation, add the images field, and save the changes. Execute the command
php artisan migrate
Output
Migrating: 2018_03_21_225819_add_images_to_articles_table Migrated: 2018_03_21_225819_add_images_to_articles_table
At this time, if you look at the data table migrations, you will find an extra row of records.
Then synchronize the changes to the production environment. The only difference is that the production server will ask you to confirm whether to execute the command.
************************************** * Application In Production! * ************************************** Do you really wish to run this command? (yes/no) [no]: > yes Migrating: 2018_03_21_225819_add_images_to_articles_table Migrated: 2018_03_21_225819_add_images_to_articles_table
Modify fields
This hasty release caused 500 errors
SQLSTATE[HY000]: General error: 1364 Field 'images' doesn't have a default value
You only need to create a new migration file and add it
$table->text('images')->nullable()->change();
Execute migrate again online to solve the problem.
Related recommendations: The latest five Laravel video tutorials
The above is the detailed content of How to change the table structure in laravel. For more information, please follow other related articles on the PHP Chinese website!