#34 Generate UUID on seeder/models

This commit is contained in:
Nabeel Shahzad 2017-06-24 13:20:24 -05:00
parent 2aa7cc66d5
commit ba57cef0a7
9 changed files with 103 additions and 14 deletions

View File

@ -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'));
}

View File

@ -11,7 +11,10 @@ use Eloquent as Model;
*/
class Flight extends Model
{
use Uuids;
public $table = 'flights';
public $incrementing = false;
protected $dates = ['deleted_at'];

View File

@ -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
View 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;
});
}
}

View File

@ -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']);

View File

@ -21,7 +21,8 @@
"symfony/yaml": "^3.3",
"league/geotools": "@stable",
"toin0u/geotools-laravel": "^1.0",
"anlutro/l4-settings": "^0.5.0"
"anlutro/l4-settings": "^0.5.0",
"webpatser/laravel-uuid": "^2.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",

49
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "abd8dd8b12ea7f677c5bf93da12e0948",
"content-hash": "7525e3933b2ad8a1f88d01bb12263d86",
"packages": [
{
"name": "anlutro/l4-settings",
@ -3944,6 +3944,53 @@
],
"time": "2016-09-01T10:05:43+00:00"
},
{
"name": "webpatser/laravel-uuid",
"version": "2.0.1",
"source": {
"type": "git",
"url": "https://github.com/webpatser/laravel-uuid.git",
"reference": "6ed2705775e3edf066b90e1292f76f157ec00507"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webpatser/laravel-uuid/zipball/6ed2705775e3edf066b90e1292f76f157ec00507",
"reference": "6ed2705775e3edf066b90e1292f76f157ec00507",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"fzaninotto/faker": "1.5.*",
"phpunit/phpunit": "4.7.*"
},
"suggest": {
"paragonie/random_compat": "A random_bytes Php 5.x polyfill."
},
"type": "library",
"autoload": {
"psr-0": {
"Webpatser\\Uuid": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Christoph Kempen",
"email": "christoph@downsized.nl"
}
],
"description": "Class to generate a UUID according to the RFC 4122 standard. Support for version 1, 3, 4 and 5 UUID are built-in.",
"homepage": "https://github.com/webpatser/uuid",
"keywords": [
"UUID RFC4122"
],
"time": "2016-05-09T09:22:18+00:00"
},
{
"name": "willdurand/geocoder",
"version": "v3.3.2",

View File

@ -18,7 +18,7 @@ class DatabaseSeeder extends Seeder
}
$svc = app('App\Services\DatabaseService');
$svc->seed_from_yaml($path);
$svc->seed_from_yaml_file($path);
}
}

View File

@ -13,6 +13,7 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/phpoffice/phpexcel" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php56" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
<excludeFolder url="file://$MODULE_DIR$/vendor/webpatser/laravel-uuid" />
<excludeFolder url="file://$MODULE_DIR$/vendor/yajra/laravel-datatables-oracle" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
@ -41,6 +42,7 @@
<root url="file://$MODULE_DIR$/vendor/phpoffice/phpexcel" />
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php56" />
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
<root url="file://$MODULE_DIR$/vendor/webpatser/laravel-uuid" />
<root url="file://$MODULE_DIR$/vendor/yajra/laravel-datatables-oracle" />
</CLASSES>
<JAVADOC />
@ -56,6 +58,7 @@
<root url="file://$MODULE_DIR$/vendor/phpoffice/phpexcel" />
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php56" />
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
<root url="file://$MODULE_DIR$/vendor/webpatser/laravel-uuid" />
<root url="file://$MODULE_DIR$/vendor/yajra/laravel-datatables-oracle" />
</SOURCES>
</library>