diff options
Diffstat (limited to 'db/migrations')
-rw-r--r-- | db/migrations/2018_10_01_000000_create_users_tables.php | 14 | ||||
-rw-r--r-- | db/migrations/Reference.php | 36 |
2 files changed, 37 insertions, 13 deletions
diff --git a/db/migrations/2018_10_01_000000_create_users_tables.php b/db/migrations/2018_10_01_000000_create_users_tables.php index 55ee382e..d6778c52 100644 --- a/db/migrations/2018_10_01_000000_create_users_tables.php +++ b/db/migrations/2018_10_01_000000_create_users_tables.php @@ -16,6 +16,7 @@ use stdClass; class CreateUsersTables extends Migration { use ChangesReferences; + use Reference; /** * Run the migration @@ -273,17 +274,4 @@ class CreateUsersTables extends Migration $this->schema->drop('users'); } - - /** - * @param Blueprint $table - */ - protected function referencesUser(Blueprint $table) - { - $table->unsignedInteger('user_id'); - - $table->primary('user_id'); - $table->foreign('user_id') - ->references('id')->on('users') - ->onDelete('cascade'); - } } diff --git a/db/migrations/Reference.php b/db/migrations/Reference.php new file mode 100644 index 00000000..4c35b59b --- /dev/null +++ b/db/migrations/Reference.php @@ -0,0 +1,36 @@ +<?php + +namespace Engelsystem\Migrations; + +use Illuminate\Database\Schema\Blueprint; + +trait Reference +{ + /** + * @param Blueprint $table + * @param bool $setPrimary + */ + protected function referencesUser(Blueprint $table, $setPrimary = true) + { + $this->references($table, 'users', 'user_id', $setPrimary); + } + + /** + * @param Blueprint $table + * @param string $targetTable + * @param string $fromColumn + * @param bool $setPrimary + */ + protected function references(Blueprint $table, $targetTable, $fromColumn, $setPrimary = false) + { + $table->unsignedInteger($fromColumn); + + if ($setPrimary) { + $table->primary($fromColumn); + } + + $table->foreign($fromColumn) + ->references('id')->on($targetTable) + ->onDelete('cascade'); + } +} |