Home > Article > PHP Framework > Database Migration and Population with Laravel: Managing Data Structure Changes
Using Laravel for database migration and filling: managing data structure changes
When developing web applications, the database is an essential part. As projects iterate and requirements change, the structure of the database will continue to change. In order to facilitate the management and maintenance of database structure changes, Laravel provides two functions: database migration and filling.
Database migration is a method of managing database structure changes using code. It allows you to create, modify, or delete database structures such as tables, fields, and indexes by writing re-runable migration scripts. Database population is the method used to add initial data to the database. Population allows you to automatically insert specific test data into the database after each migration.
Below we use a simple example to demonstrate how to use Laravel's database migration and filling functions.
First, open the terminal and go to the root directory of your Laravel project. We first need to create a migration that creates a table called "users".
php artisan make:migration create_users_table --create=users
After running the above command, Laravel will generate a new migration file in the database/migrations
directory. The file name will be the current timestamp plus create_users_table
. Next, we open the generated migration file, find the up
method, and fill in the following code:
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateSupportFacadesSchema; class CreateUsersTable extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('users'); } }
In the above code, we use the Schema
class to create A users
table is created, which contains id
, name
, email
, password
and timestamps
Five fields. The up
method is used to create the table, while the down
method is used to delete the table when rolling back the migration.
Next, we can run the following command to perform the migration:
php artisan migrate
After running the above command, Laravel will execute the migration file and create the users
table.
Next, we can create a fill file to insert some initial data into the users
table. Run the following command to create a fill file:
php artisan make:seeder UsersTableSeeder
After running the above command, Laravel will generate a new fill file in the database/seeds
directory with the file name UsersTableSeeder
.
Open the generated fill file, find the run
method, and fill in the following code:
<?php use IlluminateDatabaseSeeder; use IlluminateSupportFacadesDB; use IlluminateSupportFacadesHash; class UsersTableSeeder extends Seeder { public function run() { DB::table('users')->insert([ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => Hash::make('password123'), ]); } }
In the above code, we use the DB
class A user data is inserted, including three fields: name
, email
and password
.
Finally, we can run the fill through the following command:
php artisan db:seed --class=UsersTableSeeder
After running the above command, Laravel will execute the fill file and insert initial data into the users
table.
Through the above examples, we can see that using Laravel's database migration and filling functions, you can easily manage and maintain changes to the database structure, and you can also automatically insert initial data into the database. In this way, we can perform database operations and development work more efficiently.
To sum up, Laravel's database migration and filling functions are very useful. They can help us manage structural changes in the database and save us time and energy in manually operating the database. I hope that through the introduction of this article, readers will have a clearer understanding of how to use Laravel for database migration and filling.
The above is the detailed content of Database Migration and Population with Laravel: Managing Data Structure Changes. For more information, please follow other related articles on the PHP Chinese website!