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 quickly deal with laravel data filling and data migration

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to quickly fix laravel data filling and data migration", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to quickly fix laravel data filling and data migration.

When a project is developed, there will generally be preparation operations for project test data. In the past, sql statements were written independently based on PHP code, and then cyclically inserted into the database. When we have come into contact with the laravel database, we can easily complete the test data preparation of the project.

Data migration

1. Create a data migration file

The system has prepared a very convenient artisan command line tool for us, which can be created directly. Use the command:

# format is fixed create_ table name _ table Note the table name must be added with s, otherwise an error will be reported when performing the migration

Php artisan make:migration create_articles_table

After running the command, we can see that there is an extra file in the database/migrations folder.

To open a new file, there are two methods, up and down, that is, one corresponds to the creation of the database and the other corresponds to deletion.

two。 Modify the migration file

We can add fields to it, and I'll list the common fields for you.

$table- > id (); / / id default primary key, self-increment

$table- > tinyInteger ('cate_id'); / / equivalent to TINYINT

$table- > smallInteger ('posts'); / / equivalent to SMALLINT

$table- > integer ('votes'); / / equivalent to INT

$table- > char ('name', 4); / / fixed length string, which is equivalent to CHAR with length

$table- > string ('title', 100); / / variable length string, equivalent to VARCHAR with length

$table- > text ('description'); / / long text data

$table- > longText ('description'); / / very long text data

$table- > timestamps (); / / equivalent to nullable created_at and updated_at TIMESTAMP

There is also a "modifier" for the restricted field, which can be connected directly to the end of the field for chain operation. The commonly used modifiers are:

-> nullable () this field allows you to write NULL values

-> the unique () field is unique

-> default ($value) specifies the default value for the field

-> comment ('my comment') add comments to the field

-> autoIncrement () sets fields of type INTEGER to auto-incrementing primary key

3. Perform migration

The data table can be generated by executing the migration command.

Php artisan migrate

Note: to force a migration, add-force after the command (not recommended)

Php artisan migrate-force

4. Roll back the migration

Sometimes, when we find that the newly created migration is missing fields, or there are modifications, we can roll back and then execute the migration command again.

Php artisan migrate:rollback

The system also provides the operation of rolling back several steps, which can be added directly after-- step= steps, up to five steps.

Php artisan migrate:rollback-step=2

The system also provides several convenient migration commands:

The # migrate:reset command will roll back all the migrations of your application:

Php artisan migrate:reset

The # migrate:refresh command will execute the migrate command after rolling back all your migrations

Php artisan migrate:refresh

The # migrate:fresh command will delete all the tables in the database and then execute the migrate command

Php artisan migrate:fresh

These can be combined with the need to use their own, there is data to use refresh, there is no data, directly use fresh, more quickly.

Data filling

Note: the data padding dependency package is already built into the laravel framework, so you don't need to install it. If you need to use it in other projects, you can install it.

Composer require fzaninotto/faker

Create models and data migration

Because data padding is implemented in conjunction with the model, you will create a model here, and then include data migration and data fill files.

Php artisan make:model Models/Article-a

After modifying the migration file, perform the migration to create the data table

Php artisan migrate

Modify the ArticleFactory.php model factory file

Corresponding to the fields of the database. If the file is created manually, the use corresponding model is required.

Modify the seeder file

Add to the run method and call the model factory method you just wrote. Note that the use model is needed here.

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