Home  >  Article  >  PHP Framework  >  About laravel's enhanced Schema data migration

About laravel's enhanced Schema data migration

藏色散人
藏色散人forward
2020-09-01 13:24:401842browse

The following tutorial column of Laravel will introduce you to laravel's enhanced Schema data migration. I hope it will be helpful to friends in need!

About laravel's enhanced Schema data migration

Conception:

When we restructure the company’s project, every time we create a new migration class When creating a data table,
you need to specify the new deleted_at, created_at, updated_at, last_operater, last_operater_id
These five fields record the deletion time, new time, modification time, and last operator name of each row. , the last operator id,

Therefore, can I let him automatically create these fields without me having to manually copy and paste the code myself? Sometimes I even forget it! !

  • First time: Enhance the function of Schema, that is, enhance this function in the original command to create migration files: php artisan make:migration, but found that when laravel-admin is installed by composer , many migration files cause conflicts.
  • The second time: In order to solve this conflict problem, a second test was conducted: Since the function of Schema cannot be enhanced on the original command, let's add a command php artisan make: app_migration

However, the following is an explanation of the second test.

1. As shown below, a new Schema class is added under app/Facade, which defines two methods for obtaining mysql links. This method obtains the db_extra object from the container

About laravels enhanced Schema data migration

2. Then add a db server provider under app/providers/ to define how the $app['db_extra'] service container obtains this object

About laravels enhanced Schema data migration

3. Analyze the content in Figure 2 below. ->single Single instance is a factory class used to produce the db_extra class, as shown in Figure 3. This factory class is placed in the following figure

About laravels enhanced Schema data migration

##4. Next, analyze This factory class inherits the system's production factory class and modifies its function. For example, this time, if it is a connected mysql service connection object, I will let it instantiate the msyql connection object I defined, as shown in Figure 4

About laravels enhanced Schema data migration

5. Next, in this MysqlExtraConnection.php class, I override the method of the parent class again. When executing the command to add a new migration class , create a migration file according to the migration template I defined

About laravels enhanced Schema data migration

#6. Next, define the fields we want to add by default, as shown in Figure 6

About laravels enhanced Schema data migration

About laravels enhanced Schema data migration

#7. Next, define a command called make:app_migration. Note that in the second picture below, MigrationCreatorExt depends on injecting its own new class. What is this class used for? Used to output characters to the new migration file,

About laravels enhanced Schema data migration

About laravels enhanced Schema data migration

8. Create a template and modify it, as shown below

About laravels enhanced Schema data migration

9. In the create.stub template, I let it call the Schema class I defined by default, and noted in the comments that the fields added by default

About laravels enhanced Schema data migration

10. It’s done. It’s a bit troublesome to configure, but it’s really easy to expand. Is there any better way? Thanks for discussing in the comments, hehe

The above is the detailed content of About laravel's enhanced Schema data migration. For more information, please follow other related articles on the PHP Chinese website!

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