Data Seeder
Laravel adds the ability to seed the database with sample data. All seed classes are located in the database->seeder directory.
When you copy
Week Day
php artisan make:seeder WeekDaySeeder
Note: Please use the copy button to copy the source code.
Replace the WeekDaySeeder run content with the following code.
We need to include the following import statement(s)
use Illuminate\Support\Facades\DB;
class WeekDaySeeder extends Seeder
{
public function run()
{
DB::table('week_days')->delete();
$week_days = [
['dayValue' => 0, 'dayName' => 'Sunday'],
['dayValue' => 1, 'dayName' => 'Monday'],
['dayValue' => 2, 'dayName' => 'Tuesday'],
['dayValue' => 3, 'dayName' => 'Wednesday'],
['dayValue' => 4, 'dayName' => 'Thursday'],
['dayValue' => 5, 'dayName' => 'Friday'],
['dayValue' => 6, 'dayName' => 'Saturday'],
];
DB::table('week_days')->insert($week_days);
}
}
Month Seeder
php artisan make:seeder MonthSeeder
Note: Please use the copy button to copy the source code.
We need to include the following import statement(s)
use Illuminate\Support\Facades\DB;
Replace the MonthSeeder run content with the following code.
class MonthSeeder extends Seeder
{
public function run()
{
//
DB::table('months')->delete();
$months = [
['month' => 1, 'monthName' => 'January'],
['month' => 2, 'monthName' => 'February'],
['month' => 3, 'monthName' => 'March'],
['month' => 4, 'monthName' => 'April'],
['month' => 5, 'monthName' => 'May'],
['month' => 6, 'monthName' => 'June'],
['month' => 7, 'monthName' => 'July'],
['month' => 8, 'monthName' => 'August'],
['month' => 9, 'monthName' => 'September'],
['month' => 10, 'monthName' => 'October'],
['month' => 11, 'monthName' => 'November'],
['month' => 12, 'monthName' => 'December'],
];
DB::table('months')->insert($months);
}
}
Country Seeder
php artisan make:seeder CountrySeeder
Note: Please use the copy button to copy the source code.
We need to include the following import statement(s)
use Illuminate\Support\Facades\DB;
Replace the CountrySeeder content run with the following code.
class CountrySeeder extends Seeder
{
public function run()
{
DB::table('countries')->delete();
$countries = [
['name' => 'United Kingdom', 'code' => 'GB'],
['name' => 'United States', 'code' => 'US'],
];
DB::table('countries')->insert($countries);
}
}
User Seeder
php artisan make:seeder UserSeeder
Note: Please use the copy button to copy the source code.
We need to include the following import statement(s)
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
Replace the UserSeeder content run with the following code.
class UserSeeder extends Seeder
{
public function run()
{
DB::table('companies')->delete();
DB::table('users')->delete();
DB::table('function_role_users')->delete();
$company = [
['name' => 'ABC', 'addressLine1' => 'address line1', 'country_id'=>1, 'telephone'=>12345678, 'emailAddress'=> '[email protected]']
];
DB::table('companies')->insert($company);
$user = [
['password' => Hash::make('admin'), 'joinDate'=>date('Y-m-d'), 'email' => '[email protected]', 'name'=> 'admin', 'staffId'=> '123', 'company_id'=> 1]
];
DB::table('users')->insert($user);
$permission = [
['user_id'=> 1, 'code'=> 'setting_company_records_create'],
['user_id'=> 1, 'code'=> 'setting_company_records_read'],
['user_id'=> 1, 'code'=> 'setting_company_records_update'],
['user_id'=> 1, 'code'=> 'setting_company_records_delete'],
['user_id'=> 1, 'code'=> 'setting_company_records_restore'],
['user_id'=> 1, 'code'=> 'setting_holiday_records_create'],
['user_id'=> 1, 'code'=> 'setting_holiday_records_read'],
['user_id'=> 1, 'code'=> 'setting_holiday_records_update'],
['user_id'=> 1, 'code'=> 'setting_holiday_records_delete'],
['user_id'=> 1, 'code'=> 'setting_holiday_records_restore'],
['user_id'=> 1, 'code'=> 'setting_workingday_records_create'],
['user_id'=> 1, 'code'=> 'setting_workingday_records_read'],
['user_id'=> 1, 'code'=> 'setting_workingday_records_update'],
['user_id'=> 1, 'code'=> 'setting_workingday_records_delete'],
['user_id'=> 1, 'code'=> 'setting_workingday_records_restore'],
['user_id'=> 1, 'code'=> 'setting_department_records_create'],
['user_id'=> 1, 'code'=> 'setting_department_records_read'],
['user_id'=> 1, 'code'=> 'setting_department_records_update'],
['user_id'=> 1, 'code'=> 'setting_department_records_delete'],
['user_id'=> 1, 'code'=> 'setting_department_records_restore'],
['user_id'=> 1, 'code'=> 'setting_leavetype_records_create'],
['user_id'=> 1, 'code'=> 'setting_leavetype_records_read'],
['user_id'=> 1, 'code'=> 'setting_leavetype_records_update'],
['user_id'=> 1, 'code'=> 'setting_leavetype_records_delete'],
['user_id'=> 1, 'code'=> 'setting_leavetype_records_restore'],
['user_id'=> 1, 'code'=> 'setting_calendar_records_create'],
['user_id'=> 1, 'code'=> 'setting_calendar_records_read'],
['user_id'=> 1, 'code'=> 'setting_userAccount_records_create'],
['user_id'=> 1, 'code'=> 'setting_userAccount_records_read'],
['user_id'=> 1, 'code'=> 'setting_userAccount_records_update'],
['user_id'=> 1, 'code'=> 'setting_userAccount_records_delete'],
['user_id'=> 1, 'code'=> 'setting_userAccount_records_restore'],
['user_id'=> 1, 'code'=> 'setting_userAccount_records_change_password'],
['user_id'=> 1, 'code'=> 'setting_userAccount_records_apply_permission'],
['user_id'=> 1, 'code'=> 'setting_leave_entitlements_records_read'],
['user_id'=> 1, 'code'=> 'setting_leave_entitlements_records_update'],
['user_id'=> 1, 'code'=> 'setting_leave_entitlements_records_delete'],
['user_id'=> 1, 'code'=> 'setting_leave_type_approving_records_read'],
['user_id'=> 1, 'code'=> 'setting_leave_type_approving_records_update'],
['user_id'=> 1, 'code'=> 'setting_leave_type_approving_records_delete'],
['user_id'=> 1, 'code'=> 'setting_user_account_approving_records_read'],
['user_id'=> 1, 'code'=> 'setting_user_account_approving_records_update'],
['user_id'=> 1, 'code'=> 'setting_user_account_approving_records_delete'],
['user_id'=> 1, 'code'=> 'leave_application_apply'],
['user_id'=> 1, 'code'=> 'leave_application_manage'],
['user_id'=> 1, 'code'=> 'leave_application_history'],
['user_id'=> 1, 'code'=> 'application_menu'],
['user_id'=> 1, 'code'=> 'setting_menu'],
];
DB::table('function_role_users')->insert($permission);
}
}
Once all the seeders are created, we can run seeder script to insert the record to the database
php artisan db:seed --class=CountrySeeder
php artisan db:seed --class=WeekDaySeeder
php artisan db:seed --class=MonthSeeder
php artisan db:seed --class=UserSeeder