#34 Generate UUID on seeder/models
This commit is contained in:
@@ -53,11 +53,9 @@ class AircraftController extends BaseController
|
||||
public function store(CreateAircraftRequest $request)
|
||||
{
|
||||
$input = $request->all();
|
||||
|
||||
$aircraft = $this->aircraftRepository->create($input);
|
||||
|
||||
Flash::success('Aircraft saved successfully.');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
@@ -87,7 +85,6 @@ class AircraftController extends BaseController
|
||||
|
||||
if (empty($aircraft)) {
|
||||
Flash::error('Aircraft not found');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
@@ -106,14 +103,12 @@ class AircraftController extends BaseController
|
||||
|
||||
if (empty($aircraft)) {
|
||||
Flash::error('Aircraft not found');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
$aircraft = $this->aircraftRepository->update($request->all(), $id);
|
||||
|
||||
Flash::success('Aircraft updated successfully.');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
@@ -126,14 +121,12 @@ class AircraftController extends BaseController
|
||||
|
||||
if (empty($aircraft)) {
|
||||
Flash::error('Aircraft not found');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
$this->aircraftRepository->delete($id);
|
||||
|
||||
Flash::success('Aircraft deleted successfully.');
|
||||
|
||||
return redirect(route('admin.aircraft.index'));
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,10 @@ use Eloquent as Model;
|
||||
*/
|
||||
class Flight extends Model
|
||||
{
|
||||
use Uuids;
|
||||
|
||||
public $table = 'flights';
|
||||
public $incrementing = false;
|
||||
|
||||
protected $dates = ['deleted_at'];
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ use Illuminate\Contracts\Auth\CanResetPassword;
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use Uuids;
|
||||
use Notifiable;
|
||||
use EntrustUserTrait;
|
||||
|
||||
|
||||
21
app/Models/UuidTrait.php
Normal file
21
app/Models/UuidTrait.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Webpatser\Uuid\Uuid;
|
||||
|
||||
trait Uuids
|
||||
{
|
||||
protected static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
$key = $model->getKeyName();
|
||||
if (empty($model->{$key})) {
|
||||
$model->{$key} = Uuid::generate()->string;
|
||||
}
|
||||
#$model->{$model->getKeyName()} = Uuid::generate()->string;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,25 +3,45 @@
|
||||
namespace App\Services;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Webpatser\Uuid\Uuid;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
|
||||
class DatabaseService extends BaseService {
|
||||
class DatabaseService extends BaseService
|
||||
{
|
||||
|
||||
protected $uuid_tables = [
|
||||
'flights',
|
||||
'users',
|
||||
];
|
||||
|
||||
protected function time(): string
|
||||
{
|
||||
return Carbon::now('UTC')->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
public function seed_from_yaml($yaml_file)
|
||||
public function seed_from_yaml_file($yaml_file)
|
||||
{
|
||||
$yml = file_get_contents($yaml_file);
|
||||
$this->seed_from_yaml($yml);
|
||||
}
|
||||
|
||||
public function seed_from_yaml($yml)
|
||||
{
|
||||
$time_fields = ['created_at', 'updated_at'];
|
||||
|
||||
$yml = Yaml::parse(file_get_contents($yaml_file));
|
||||
$yml = Yaml::parse($yml);
|
||||
foreach ($yml as $table => $rows) {
|
||||
foreach ($rows as $row) {
|
||||
|
||||
# see if this table uses a UUID as the PK
|
||||
# if no ID is specified
|
||||
if(in_array($table, $this->uuid_tables)) {
|
||||
if(!array_key_exists('id', $row)) {
|
||||
$row['id'] = Uuid::generate()->string;
|
||||
}
|
||||
}
|
||||
|
||||
# encrypt any password fields
|
||||
if(array_key_exists('password', $row)) {
|
||||
$row['password'] = bcrypt($row['password']);
|
||||
|
||||
Reference in New Issue
Block a user