Migration: Moved reference method to trait

This commit is contained in:
Igor Scheller 2018-11-10 20:42:48 +01:00 committed by msquare
parent 720b46f60f
commit 951828a4f1
2 changed files with 37 additions and 13 deletions

View File

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

View File

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