phpvms/database/seeds/DevelopmentSeeder.php

49 lines
1.2 KiB
PHP
Raw Normal View History

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