Seeders in Laravel allow you to populate your database with test data quickly. This is especially useful when you want to test your application or provide initial data for tables like schools and students in your School ERP.
Steps to Use Seeders in Laravel:
Create a Seeder: You can create a seeder for each table. For example, let’s create seeders for tbl_school_master and tbl_student_master.
Run the following Artisan commands:
php artisan make:seeder SchoolMasterSeeder
php artisan make:seeder StudentMasterSeeder
This will generate two seeder files in the database/seeders directory.
Define the Seeder Logic: Open each seeder file and define how to populate the table with data.
For SchoolMasterSeeder.php:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class SchoolMasterSeeder extends Seeder
{
public function run()
{
DB::table(‘tbl_school_master’)->insert([
[
‘school_name’ => ‘Green Valley High School’,
‘school_email’ => ‘contact@greenvalley.edu’,
‘school_password’ => bcrypt(‘password123’),
‘school_fysm’ => ‘April’,
‘city’ => ‘New York’,
‘country’ => ‘USA’,
‘pincode’ => ‘10001’,
‘phone’ => ‘1234567890’,
‘currency’ => ‘USD’,
‘created_on’ => now(),
],
[
‘school_name’ => ‘Blue Ridge Academy’,
‘school_email’ => ‘info@blueridge.edu’,
‘school_password’ => bcrypt(‘password123’),
‘school_fysm’ => ‘July’,
‘city’ => ‘Los Angeles’,
‘country’ => ‘USA’,
‘pincode’ => ‘90001’,
‘phone’ => ‘9876543210’,
‘currency’ => ‘USD’,
‘created_on’ => now(),
],
]);
}
}
For StudentMasterSeeder.php:
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class StudentMasterSeeder extends Seeder
{
public function run()
{
DB::table('tbl_student_master')->insert([
[
'student_name' => 'John Doe',
'roll_no' => '1001',
'reg_no' => 'REG1234',
'school_id' => 1, // Assuming Green Valley High School has school_id 1
'student_phone' => '1234567890',
'student_email' => 'john.doe@example.com',
'parent_name' => 'Jane Doe',
'date_of_birth' => '2005-06-15',
'type' => 'day',
'gender' => 'male',
'religion' => 'Christian',
'caste' => 'General',
'password' => bcrypt('password123'),
'active' => true,
'created_on' => now(),
'admission_date' => '2020-09-01',
'photograph' => 'photos/john_doe.jpg',
],
[
'student_name' => 'Emily Smith',
'roll_no' => '1002',
'reg_no' => 'REG5678',
'school_id' => 2, // Assuming Blue Ridge Academy has school_id 2
'student_phone' => '9876543210',
'student_email' => 'emily.smith@example.com',
'parent_name' => 'Richard Smith',
'date_of_birth' => '2006-03-22',
'type' => 'boarding',
'gender' => 'female',
'religion' => 'Christian',
'caste' => 'General',
'password' => bcrypt('password123'),
'active' => true,
'created_on' => now(),
'admission_date' => '2021-06-10',
'photograph' => 'photos/emily_smith.jpg',
],
]);
}
}
Register Seeders in DatabaseSeeder.php: In the DatabaseSeeder.php file (also located in database/seeders), you need to call the seeders you created. Open the file and update it like this:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
SchoolMasterSeeder::class,
StudentMasterSeeder::class,
]);
}
}
Run the Seeder: Now, run the seeders to populate the database with data. Use the following Artisan command:
php artisan db:seed
This will execute all the seeders listed in DatabaseSeeder.php and populate the tbl_school_master and tbl_student_master tables with sample data.
Check the Database: After running the seeders, you can check your database (using a tool like phpMyAdmin, TablePlus, or MySQL Workbench) to see the inserted data.
Notes:
Password Hashing: In both seeders, the passwords are hashed using bcrypt to ensure security. Foreign Keys: Make sure that the school_id in the tbl_student_master corresponds to an existing school in the tbl_school_master table. You can adjust these manually or use factories for dynamic data. Use Faker: If you want to seed more data, you can use Laravel’s Faker library to generate random data for testing.