#34 Generate UUID on seeder/models
This commit is contained in:
@@ -53,11 +53,9 @@ class AircraftController extends BaseController
|
|||||||
public function store(CreateAircraftRequest $request)
|
public function store(CreateAircraftRequest $request)
|
||||||
{
|
{
|
||||||
$input = $request->all();
|
$input = $request->all();
|
||||||
|
|
||||||
$aircraft = $this->aircraftRepository->create($input);
|
$aircraft = $this->aircraftRepository->create($input);
|
||||||
|
|
||||||
Flash::success('Aircraft saved successfully.');
|
Flash::success('Aircraft saved successfully.');
|
||||||
|
|
||||||
return redirect(route('admin.aircraft.index'));
|
return redirect(route('admin.aircraft.index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +85,6 @@ class AircraftController extends BaseController
|
|||||||
|
|
||||||
if (empty($aircraft)) {
|
if (empty($aircraft)) {
|
||||||
Flash::error('Aircraft not found');
|
Flash::error('Aircraft not found');
|
||||||
|
|
||||||
return redirect(route('admin.aircraft.index'));
|
return redirect(route('admin.aircraft.index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,14 +103,12 @@ class AircraftController extends BaseController
|
|||||||
|
|
||||||
if (empty($aircraft)) {
|
if (empty($aircraft)) {
|
||||||
Flash::error('Aircraft not found');
|
Flash::error('Aircraft not found');
|
||||||
|
|
||||||
return redirect(route('admin.aircraft.index'));
|
return redirect(route('admin.aircraft.index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$aircraft = $this->aircraftRepository->update($request->all(), $id);
|
$aircraft = $this->aircraftRepository->update($request->all(), $id);
|
||||||
|
|
||||||
Flash::success('Aircraft updated successfully.');
|
Flash::success('Aircraft updated successfully.');
|
||||||
|
|
||||||
return redirect(route('admin.aircraft.index'));
|
return redirect(route('admin.aircraft.index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,14 +121,12 @@ class AircraftController extends BaseController
|
|||||||
|
|
||||||
if (empty($aircraft)) {
|
if (empty($aircraft)) {
|
||||||
Flash::error('Aircraft not found');
|
Flash::error('Aircraft not found');
|
||||||
|
|
||||||
return redirect(route('admin.aircraft.index'));
|
return redirect(route('admin.aircraft.index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->aircraftRepository->delete($id);
|
$this->aircraftRepository->delete($id);
|
||||||
|
|
||||||
Flash::success('Aircraft deleted successfully.');
|
Flash::success('Aircraft deleted successfully.');
|
||||||
|
|
||||||
return redirect(route('admin.aircraft.index'));
|
return redirect(route('admin.aircraft.index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ use Eloquent as Model;
|
|||||||
*/
|
*/
|
||||||
class Flight extends Model
|
class Flight extends Model
|
||||||
{
|
{
|
||||||
|
use Uuids;
|
||||||
|
|
||||||
public $table = 'flights';
|
public $table = 'flights';
|
||||||
|
public $incrementing = false;
|
||||||
|
|
||||||
protected $dates = ['deleted_at'];
|
protected $dates = ['deleted_at'];
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ use Illuminate\Contracts\Auth\CanResetPassword;
|
|||||||
*/
|
*/
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
|
use Uuids;
|
||||||
use Notifiable;
|
use Notifiable;
|
||||||
use EntrustUserTrait;
|
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;
|
namespace App\Services;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Webpatser\Uuid\Uuid;
|
||||||
use Symfony\Component\Yaml\Yaml;
|
use Symfony\Component\Yaml\Yaml;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
|
||||||
class DatabaseService extends BaseService {
|
class DatabaseService extends BaseService
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $uuid_tables = [
|
||||||
|
'flights',
|
||||||
|
'users',
|
||||||
|
];
|
||||||
|
|
||||||
protected function time(): string
|
protected function time(): string
|
||||||
{
|
{
|
||||||
return Carbon::now('UTC')->format('Y-m-d H:i:s');
|
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'];
|
$time_fields = ['created_at', 'updated_at'];
|
||||||
|
$yml = Yaml::parse($yml);
|
||||||
$yml = Yaml::parse(file_get_contents($yaml_file));
|
|
||||||
foreach ($yml as $table => $rows) {
|
foreach ($yml as $table => $rows) {
|
||||||
foreach ($rows as $row) {
|
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
|
# encrypt any password fields
|
||||||
if(array_key_exists('password', $row)) {
|
if(array_key_exists('password', $row)) {
|
||||||
$row['password'] = bcrypt($row['password']);
|
$row['password'] = bcrypt($row['password']);
|
||||||
|
|||||||
@@ -21,7 +21,8 @@
|
|||||||
"symfony/yaml": "^3.3",
|
"symfony/yaml": "^3.3",
|
||||||
"league/geotools": "@stable",
|
"league/geotools": "@stable",
|
||||||
"toin0u/geotools-laravel": "^1.0",
|
"toin0u/geotools-laravel": "^1.0",
|
||||||
"anlutro/l4-settings": "^0.5.0"
|
"anlutro/l4-settings": "^0.5.0",
|
||||||
|
"webpatser/laravel-uuid": "^2.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"fzaninotto/faker": "~1.4",
|
"fzaninotto/faker": "~1.4",
|
||||||
|
|||||||
49
composer.lock
generated
49
composer.lock
generated
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "abd8dd8b12ea7f677c5bf93da12e0948",
|
"content-hash": "7525e3933b2ad8a1f88d01bb12263d86",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "anlutro/l4-settings",
|
"name": "anlutro/l4-settings",
|
||||||
@@ -3944,6 +3944,53 @@
|
|||||||
],
|
],
|
||||||
"time": "2016-09-01T10:05:43+00:00"
|
"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",
|
"name": "willdurand/geocoder",
|
||||||
"version": "v3.3.2",
|
"version": "v3.3.2",
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
}
|
}
|
||||||
|
|
||||||
$svc = app('App\Services\DatabaseService');
|
$svc = app('App\Services\DatabaseService');
|
||||||
$svc->seed_from_yaml($path);
|
$svc->seed_from_yaml_file($path);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpoffice/phpexcel" />
|
<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-php56" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
|
<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" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/yajra/laravel-datatables-oracle" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
@@ -41,6 +42,7 @@
|
|||||||
<root url="file://$MODULE_DIR$/vendor/phpoffice/phpexcel" />
|
<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-php56" />
|
||||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
|
<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" />
|
<root url="file://$MODULE_DIR$/vendor/yajra/laravel-datatables-oracle" />
|
||||||
</CLASSES>
|
</CLASSES>
|
||||||
<JAVADOC />
|
<JAVADOC />
|
||||||
@@ -56,6 +58,7 @@
|
|||||||
<root url="file://$MODULE_DIR$/vendor/phpoffice/phpexcel" />
|
<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-php56" />
|
||||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-util" />
|
<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" />
|
<root url="file://$MODULE_DIR$/vendor/yajra/laravel-datatables-oracle" />
|
||||||
</SOURCES>
|
</SOURCES>
|
||||||
</library>
|
</library>
|
||||||
|
|||||||
Reference in New Issue
Block a user