2018-11-10 20:42:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Migrations;
|
|
|
|
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
2019-12-03 20:09:22 +01:00
|
|
|
use Illuminate\Database\Schema\ColumnDefinition;
|
2019-11-27 23:43:21 +01:00
|
|
|
use Illuminate\Support\Str;
|
2018-11-10 20:42:48 +01:00
|
|
|
|
|
|
|
trait Reference
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param Blueprint $table
|
|
|
|
* @param bool $setPrimary
|
|
|
|
*/
|
2019-11-27 23:43:21 +01:00
|
|
|
protected function referencesUser(Blueprint $table, bool $setPrimary = false)
|
2018-11-10 20:42:48 +01:00
|
|
|
{
|
2019-11-27 23:43:21 +01:00
|
|
|
$this->references($table, 'users', null, $setPrimary);
|
2018-11-10 20:42:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-27 23:43:21 +01:00
|
|
|
* @param Blueprint $table
|
|
|
|
* @param string $targetTable
|
|
|
|
* @param string|null $fromColumn
|
|
|
|
* @param bool $setPrimary
|
2019-12-03 20:09:22 +01:00
|
|
|
* @return ColumnDefinition
|
2018-11-10 20:42:48 +01:00
|
|
|
*/
|
2019-11-27 23:43:21 +01:00
|
|
|
protected function references(
|
|
|
|
Blueprint $table,
|
|
|
|
string $targetTable,
|
|
|
|
?string $fromColumn = null,
|
|
|
|
bool $setPrimary = false
|
|
|
|
): ColumnDefinition {
|
|
|
|
$fromColumn = $fromColumn ?? Str::singular($targetTable) . '_id';
|
2019-12-03 20:09:22 +01:00
|
|
|
$col = $table->unsignedInteger($fromColumn);
|
2018-11-10 20:42:48 +01:00
|
|
|
|
|
|
|
if ($setPrimary) {
|
|
|
|
$table->primary($fromColumn);
|
|
|
|
}
|
|
|
|
|
|
|
|
$table->foreign($fromColumn)
|
|
|
|
->references('id')->on($targetTable)
|
2019-09-29 14:00:47 +02:00
|
|
|
->onUpdate('cascade')
|
2018-11-10 20:42:48 +01:00
|
|
|
->onDelete('cascade');
|
2019-12-03 20:09:22 +01:00
|
|
|
|
|
|
|
return $col;
|
2018-11-10 20:42:48 +01:00
|
|
|
}
|
|
|
|
}
|