Laravel implements database migration and supports Chinese filling, while laravel supports Chinese.
Preface
Database migration is actually to control the version of the database table structure changes. The method of modifying the database table structure is relatively primitive. For example, a field is added to a database table, execute alter table xxx add directly in the database table .. but this method has some drawbacks. For example, in the development stage, you have modified your own database and table, and you have to pass this SQL statement to others for execution, this is not a good method for collaborative development. is there a way for us to make some simple version control for the modification of database tables, while also allowing others to easily synchronize our changes to the database?
The answer is that we can use Laravel's built-in Migrations.
What is the management of the database?
Laravel manages database versions in two departments: database structure management and data management.
- Database Structure Management: It mainly manages the database structure, such as adding a table and adding a field to a table.
- Data Management: This mainly manages the data in the table and generates some fill data to solve the problem of no test data during development and debugging.
We often conduct collaborative development by project teams. Everyone is in their own local database. If you have asked your colleagues to manually add fields to the database structure, database migration can solve this problem.
In addition, online deployment also avoids the trouble of manually importing a database or manually modifying the data structure. data migration helps you easily maintain the data structure.
Data filling makes it easy to batch fill a large amount of data without having to create data one by one during testing.
This article is based on Laravel5.5 and similar to other versions.
Data Migration
If we need a student table, we will not use native SQL statements to create the table.
Create a migration File
The premise is that the database connection information has been configured.
php artisan make:migration create_students_table
This command will generate a file similar to pai_10_28_035802_create_students_table.php in the database/migrations/directory.
Add the data structure of the students table
<? Phpuse Illuminate \ Database \ Migrations \ Migration; use Illuminate \ Database \ Schema \ Blueprint; use Illuminate \ Support \ Facades \ Schema; class CreateStudentsTable extends Migration {/*** Run the migrations. ** @ return void */public function up () {// students indicates the table name Schema: create ('students', function (Blueprint $ table) {// storage engine $ table-> engine = 'innodb'; // id auto-increment $ table-> increments ('id '); // Student name $ table-> string ('name'); // gender $ table-> string ('sex '); // email $ table-> string ('email '); // favorite color $ table-> string ('favorite _ color '); // mobile phone number $ table-> string ('phone'); // address $ table-> string ('addr '); // automatically maintain the timestamp $ table-> timestamps () ;}/ *** Reverse the migrations. ** @ return void */public function down () {Schema: dropIfExists ('students ');}}
For more information, see the official manual.
Run migration
php artisan migrate
In this way, all migration files in the database/migrations/directory are run, and the migrations table is created automatically to record the running migration files to prevent repeated operations.
Let's see if the database has automatically created the students table.
If the following error occurs:
[Illuminate\Database\QueryException]SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))[PDOException]SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
In the database/migrations/directory, two migration files, laravel user and password reset, will be run together.
Here we can solve this problem by modifying the mysql character set in config/database. php of the database configuration file to utf8.
'charset' => 'utf8','collation' => 'utf8_unicode_ci',
Want to know why, can stamp: http://www.bkjia.com/article/127319.htm
Data filling (Chinese supported)
Create a student table Eloquent Model
Create Student. php In the app directory
<? Phpnamespace App; use Illuminate \ Database \ Eloquent \ Model;/*** Student Model */class Student extends Model {}
Create a filling File
php artisan make:seed StudentsTableSeeder
This command will generate the StudentsTableSeeder. php filling file in the database/seeds/directory.
<? Phpuse Illuminate \ Database \ Seeder; class StudentsTableSeeder extends Seeder {/*** Run the database seeds. ** @ return void */public function run () {// call the model factory to generate 10000 pieces of data factory (App \ Student: class, 10000) -> create ();}}
Call this Seeders
Open the database/seeds/DatabaseSeeder. php file and modify it
<? Phpuse Illuminate \ Database \ Seeder; class DatabaseSeeder extends Seeder {/*** Run the database seeds. ** @ return void */public function run () {// call the student table filling File $ this-> call (StudentsTableSeeder: class );}}
Create model factory fill
php artisan make:factory StudentsFactory -m Student
This command will generate the StudentsFactory. php file in the database/factories/directory. Let's define the data format to be filled.
<? Phpuse Faker \ Generator as Faker;/* @ var Illuminate \ Database \ Eloquent \ Factory $ factory */$ factory-> define (App \ Student: class, function (Faker $ faker) {$ sex = rand (1, 1000); return ['name' => $ faker-> name, 'Sex '=> $ sex % 2 = 0? 'Male': 'female ', 'email' => $ faker-> unique ()-> safeEmail, 'favorite _ color' => $ faker-> safeColorName, 'phone' => $ faker-> phoneNumber, 'add' => $ faker-> address,];});
For more configuration information, see the file vendor/fzaninotto/faker/src/Faker/Generator. php.
Fill faker with Chinese characters
Public function boot () {// fill in Chinese data $ this-> app-> singleton (\ Faker \ Generator: class, function () {return \ Faker \ Factory :: create ('zh _ cn ');});}
Start filling
First, execute:
composer dump-autoload
Automatically load the filling file we created in the database/seeds/directory to avoid the following errors:
[ReflectionException]Class StudentsTableSeeder does not exist
Then run the fill command:
php artisan db:seed
Because we are filling in 10 thousand pieces of data, we can take a little longer, and we can refresh the database to view the added data one by one.
Success
If no error is reported in the preceding operations, check whether the students table in our database has data?
Id |
Name |
Sex |
Email |
Favorite_color |
Phone |
Addr |
Created_at |
Updated_at |
10000 |
Tan Ying |
Male |
Cum_et@example.com |
White |
17642207316 |
Hailing district, Guiyang |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9999 |
Tang shuzhen |
Male |
Qlaudantium@example.net |
Black |
18239453935 |
Nanning friendly zone |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9998 |
Jia Chunmei |
Male |
Ea35@example.com |
Corn color |
17103645128 |
Xiaoshan District, Changsha |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9997 |
Ji Zhiming |
Male |
Cdeleniti@example.com |
Gray |
17002359608 |
Huaxi district, Tianjin |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9996 |
Cheng Yan |
Male |
Aspernatur.aut@example.com |
Yellow |
17181193397 |
: 19, Xishan district, Guiyang: |
10 |
2017-10-28 05:19:10 |
9995 |
Mibo |
Male |
Reprehenderit_autem@example.com |
Purple |
17187328893 |
Guangzhou dongli District |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9994 |
Lan Shulan |
Female |
Et_ea@example.com |
Green |
18592254358 |
Lanzhou Economic Development New District |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9993 |
Leyao |
Female |
Vel.vitae@example.org |
Zang Qing |
15891490007 |
Hong Kong Longtan District: |
10 |
2017-10-28 05:19:10 |
9992 |
Ye Zhixin |
Female |
Lcumque@example.net |
Zang Qing |
15564391466 |
Beijing Gaoming District |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9991 |
Yan Yang |
Male |
Voluptatem00@example.com |
Yellow |
17097722096 |
Xincheng district, Zhengzhou |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9990 |
Ling min |
Female |
Magni22@example.org |
Fresh green |
13021578051 |
Hangzhou Fuling District |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9989 |
XI Jian |
Female |
Fugiat_accusantium@example.net |
Purple |
18070573726 |
Hailing district, Nanchang |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
9988 |
Xin Hua |
Female |
Debitis_sapiente@example.com |
Water Color |
17004061646 |
Chengdu Nanchang District |
2017-10-28 05:19:10 |
2017-10-28 05:19:10 |
......
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.