Understanding Migrations and Seeders in Laravel: A Step-by-Step Guide

Photo by Harshil Gudka on Unsplash

Understanding Migrations and Seeders in Laravel: A Step-by-Step Guide

Laravel Oct 2, 2022
đŸ’¡
Migrations in Laravel are an essential part of any web application's development process. They allow developers to create, modify, and manage the database schema in a structured and organized way. In this blog post, we will go over the basics of migrations in Laravel and how to use them effectively.

First, let's understand the concept of migrations in Laravel. Migrations are a way to create and modify the database schema in a structured and organized way. They are similar to version control for your database schema, allowing you to keep track of changes and rollback if needed.

Migrations in Laravel are defined in the 'database/migrations' folder. Each migration file has a timestamp in its name, and it contains two methods: 'up' and 'down'. The 'up' method is used to create or modify the database schema, while the 'down' method is used to rollback changes.

To create a new migration, you can use the Artisan command 'make:migration'. For example, to create a migration for a 'users' table, you can use the following command:

php artisan make:migration create_users_table

This command will create a new migration file in the 'database/migrations' folder, with the name 'create_users_table'.

Here is an example of a basic migration file to create a 'users' table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

In this example, the 'up' method is used to create a new 'users' table with an 'id', 'name', 'email', 'password', and 'timestamps' columns. The 'down' method is used to rollback the changes by dropping the 'users' table.

To run the migration, you can use the Artisan command 'migrate'. For example, to run all migrations, you can use the following command:

php artisan migrate

You can also rollback migrations by using the 'migrate:rollback' command. This command will rollback the last batch of migration.

php artisan migrate:rollback

Another important feature of migrations is the ability to seed the database with test data. Seeding is a way to insert data into the database for testing or development purposes. You can create a seeder class by using the 'make:seeder' command. For example, to create a seeder for the 'users' table, you can use the following command:

php artisan make:seeder UsersTableSeeder

This command will create a new seeder class in the 'database/seeders' folder, with the name 'UsersTableSeeder'. Here is an example of a basic seeder class:

<?
php
use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->insert([
'name' => 'John Doe',
'email' => 'johndoe@example.com',
'password' => bcrypt('password')
]);
}
}

In this example, the 'run'methodis used toinsert a newuserinto the 'users' table. To run the seeder, you can use the 'db:seed' command. For example, to run all seeders, you can use the following command:

php artisan db:seed

You can also specify a specific seeder to run by passing the seeder class name as an argument. For example, to run the 'UsersTableSeeder', you can use the following command:

php artisan db:seed --class=UsersTableSeeder

In conclusion, migrations and seeders are an essential part of any web application's development process, and Laravel provides a simple and efficient way to handle them. By understanding the basics of migrations and seeders and how to use them effectively, you can make your application more robust and maintainable.

Tags

Anurag Deep

Logical by Mind, Creative by Heart