phpvms/database/seeds/DatabaseSeeder.php

47 lines
1.2 KiB
PHP
Raw Normal View History

2017-06-09 02:28:26 +08:00
<?php
use Carbon\Carbon;
2017-06-09 02:28:26 +08:00
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
2017-06-20 00:50:25 +08:00
$this->seed_from_yaml(App::environment());
}
protected function seed_from_yaml($env): void
{
$path = database_path('seeds/'.$env.'.yml');
$time_fields = ['created_at', 'updated_at'];
$curr_time = Carbon::now('UTC')->format('Y-m-d H:i:s');
$yml = Yaml::parse(file_get_contents($path));
foreach ($yml as $table => $rows) {
foreach ($rows as $row) {
# encrypt any password fields
if (array_key_exists('password', $row)) {
$row['password'] = bcrypt($row['password']);
}
# if any time fields are == to "now", then insert the right time
foreach ($time_fields as $tf) {
if (array_key_exists($tf, $row) && $row[$tf] === 'now') {
$row[$tf] = $curr_time;
}
}
2017-06-20 00:50:25 +08:00
DB::table($table)->insert($row);
}
}
}
2017-06-09 02:28:26 +08:00
}