Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change the table structure of laravel

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces laravel how to change the table structure, the article introduces in great detail, has a certain reference value, interested friends must read it!

Laravel changes the table structure by: 1, generate migration file; 2, execute the command "php artisan make:migration..."; 3, add "$table- > text ('images')-> nullable ()." That's it.

This article operating environment: Windows7 system, Laravel5.7 version, Dell G3 computer.

Modify database table structure using laraval migration

Previously, sql files have been used to save and track changes in the structure of database tables. However, using sql files has the following disadvantages:

It is not possible to determine which sql files are executed and which are not. Although the date is added as the prefix of the sql file name, head scratches are often required when multiple developers do not update the structure of the online data table for a long time.

Executing sql files is a manual task. When you are faced with a dozen sql alter files, it is very tiring to execute them one by one.

It is still time-consuming and laborious to synchronize multiple development and production environments. For example, it's hard to think about making a change on the PC development machine and synchronizing it to the laptop development environment.

These problems are far less enjoyable than implementing one 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 the migration file and execute the command.

Php artisan make:migration add_images_to_articles_table-table=articles

The output is

Created Migration: 2018_03_21_225819_add_images_to_articles_table

A file is automatically generated under 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 prefixed with a date and time.

Public function up () {Schema::table ('articles', function (Blueprint $table) {$table- > text (' images');});}

Follow the official document, add the images field, and save the changes. Execute a command

Php artisan migrate

Output

Migrating: 2018_03_21_225819_add_images_to_articles_tableMigrated: 2018_03_21_225819_add_images_to_articles_table

At this point, if you look at the data table migrations, you will find that there is an extra row of records.

Then synchronize the changes to the production environment. The only difference is that confirmation is required on the production environment server to confirm the execution of the command.

* Application In Production! * Do you really wish to run this command? (yes/no) [no]: > yesMigrating: 2018_03_21_225819_add_images_to_articles_tableMigrated: 2018_03_21_225819_add_images_to_articles_table

Modify field

The failure to release in haste resulted in 500 errors.

SQLSTATE [HY000]: General error: 1364 Field 'images' doesn't have a default value

Just create a new migration file and join

$table- > text ('images')-> nullable ()-> change ()

The problem can be solved by performing migrate online again.

The above is all the contents of the article "how to change the table structure of laravel". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report