Hello, today I want to talk about seeder class in Laravel, in programming to write a scalable, reusable and clean code, you need to follow some programming principles.
What is an seeder?
In Laravel, a Seeder is a feature that allows you to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory.
STEP 1: Installing Laravel via Composer
You can ignore this step if you already have an application downloaded or else, run the following command to create a brand new laravel project.
composer create-project laravel/laravel {directory} 10.0 --prefer-dist
STEP 2: Configure Database Connection
In this type of project, we must give precedence to the database connection, generically it should be configured before getting started. Incorporate the following code in .env file with your database details.
DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=laravelDB_USERNAME=rootDB_PASSWORD=
STEP 3: Run Migrations in Laravel
Laravel Migration is an developer-friendly feature in Laravel that allows you to create a table in your database. It allows you to modify and share the application's database schema. You can modify the table by adding a new column or deleting an existing column.
Each migration stored in the database/migrations directory. And each migration filename contains a timestamp that allows Laravel to determine the order of the migrations.
php artisan migrate
STEP 4:Create Specific Seeder Class in Laravel
To create a specific seeder class in Laravel, you can use the make:seeder Artisan command. This command will generate a new seeder class in the database/seeders directory.
For example, if you want to create a seeder for a users table, you would run the following command in your terminal:
php artisan make:seeder UsersTableSeeder
This will create a new file UsersTableSeeder.php in the database/seeders directory. The generated seeder class will be empty apart from a run method. You can place your database seeding logic within this method.
Here's an example of what the UsersTableSeeder.php might look like:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;use App\User;
class UsersTableSeeder extends Seeder{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'name' => 'K.K ADIL',
'email' => '[email protected]',
'password' => bcrypt('password'),
]);
}}
In this example, the run method creates a new user in the users table with the name 'K.K ADIL', email '[email protected]', and password 'password'.
STEP 5: Register New Seeder Class in DatabaseSeeder.php
In the run method of the DatabaseSeeder.php file, add a call to call method with your new seeder class as an argument:
<?php
use Illuminate\\Database\\Seeder;
class DatabaseSeeder extends Seeder{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
UsersTableSeeder::class,
// your new seeder class add here
]);
}}
STEP 6: Run Seeders and Migrate
By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:
php artisan db:seed --class=ProductTableSeeder
After running the above command; you will see following message on the terminal.
# INFO Seeding database.
Database\Seeders\UsersTableSeeder .................................................. RUNNING Database\Seeders\UsersTableSeeder ............................................ 14.4 ms DONE
Now, when you run the db:seed Artisan command, Laravel will execute all the seed classes specified in the call method of the DatabaseSeeder class:
php artisan db:seed
After running the above command; you will see following message on the terminal.
# INFO Seeding database.
Database\Seeders\UsersTableSeeder .................................................. RUNNING Database\Seeders\UsersTableSeeder ............................................ 14.4 ms DONE