The goal of this tutorial is to learn how to manage Laravel's database migrations and seeders. Laravel's migrations provide mechanisms for creating and modifying database tables, while seeders allow you to fill your database with test data.
By the end of this tutorial, you'll be able to:
- Create and run migrations to manage your database schema.
- Create and run seeders to populate your database with test data.
Prerequisites:
- Basic understanding of PHP and Laravel.
- Installed Laravel environment.
Migrations are like version control for your database, allowing you to modify your database schema in a structured and easy-to-read manner. Seeders, on the other hand, are used to populate your database with data. This can be extremely useful when you want to populate your database with test data for testing purposes.
To create a migration, you can use the make:migration
Artisan command. For example, to create a migration that creates a users
table:
php artisan make:migration create_users_table
To run your migrations, use the migrate
Artisan command:
php artisan migrate
To generate a seeder, you may use the make:seeder
Artisan command. For example, to create a seeder for the users
table:
php artisan make:seeder UsersTableSeeder
To run your seeders, use the db:seed
Artisan command:
php artisan db:seed
Here are some practical examples:
Here's an example of a migration to create a users
table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Here's an example of a seeder for the users
table:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => Hash::make('password'),
]);
}
}
In this tutorial, you've learned how to create and run migrations and seeders in Laravel. You've also seen examples of how to use these features to manage your database schema and populate your database with test data.
Next steps could include exploring other features of Laravel, such as its routing, controller, or middleware features. Some recommended resources for continuing your learning include the official Laravel documentation, Laracasts, and the Laravel News blog.
phone
column to the users
table.users
table with 50 random users.posts
table, and a seeder to populate it with 100 random posts. Each post should be associated with a user.Remember, practice is key when it comes to programming. Keep at it, and you'll get the hang of Laravel's database functionality in no time!