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!
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! !
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
2. Then add a db server provider under app/providers/ to define how the $app['db_extra'] service container obtains this object
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
##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 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 #6. Next, define the fields we want to add by default, as shown in Figure 6 #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, 8. Create a template and modify it, as shown below 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 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, heheThe 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!