How To Create Database Seeder With Laravel Framework

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.

How Can I Create DataBase Seeder With Laravel FrameWork

1.Creating Migrations

To create a migration, you may use the migrate:make command on the Artisan CLI:

php artisan migrate:make create_users_table

specify a --path option when creating the migration
php artisan migrate:make foo --path=app/migrations


 

The --table and --create options may also be used to indicate the name of the table, and whether the migration will be creating a new table:

 
php artisan migrate:make add_votes_to_user_table --table=users

php artisan migrate:make create_users_table --create=users
 

2.Running Migrations
Running All Outstanding Migrations

php artisan migrate

Running All Outstanding Migrations For A Path

php artisan migrate --path=app/foo/migrations

Running All Outstanding Migrations For A Package

php artisan migrate --package=vendor/package


3.Rolling Back Migrations
Rollback The Last Migration Operation

php artisan migrate:rollback

Rollback all migrations

php artisan migrate:reset

Rollback all migrations and run them all again

php artisan migrate:refresh

php artisan migrate:refresh --seed


4.Database Seeding
Laravel also includes a simple way to seed your database with test data using seed classes. All seed classes are stored in app/database/seeds. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UserTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Example(1)

//Example Database Seed Class
class DatabaseSeeder extends Seeder {

public function run()
{
$this->call('UserTableSeeder');

$this->command->info('User table seeded!');
}

}

class UserTableSeeder extends Seeder {

public function run()
{
DB::table('users')->delete();

User::create(array('email' => 'foo@bar.com'));
}

}



To seed your database, you may use the db:seed command on the Artisan CLI:

php artisan db:seed


the --class option to specify a specific seeder class to run individually:

php artisan db:seed --class=UserTableSeeder


database using the migrate:refresh command, which will also rollback and re-run all of your migrations:

php artisan migrate:refresh --seed

 

Related Post