55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace Engelsystem\Migrations;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Database\Schema\ColumnDefinition;
|
|
use Illuminate\Support\Str;
|
|
|
|
trait Reference
|
|
{
|
|
/**
|
|
* @return ColumnDefinition
|
|
*/
|
|
protected function referencesUser(Blueprint $table, bool $setPrimary = false): ColumnDefinition
|
|
{
|
|
return $this->references($table, 'users', null, null, $setPrimary);
|
|
}
|
|
|
|
/**
|
|
* @return ColumnDefinition
|
|
*/
|
|
protected function references(
|
|
Blueprint $table,
|
|
string $targetTable,
|
|
?string $fromColumn = null,
|
|
?string $targetColumn = null,
|
|
bool $setPrimary = false,
|
|
string $type = 'unsignedInteger'
|
|
): ColumnDefinition {
|
|
$fromColumn = $fromColumn ?? Str::singular($targetTable) . '_id';
|
|
$col = $table->{$type}($fromColumn);
|
|
|
|
if ($setPrimary) {
|
|
$table->primary($fromColumn);
|
|
}
|
|
|
|
$this->addReference($table, $fromColumn, $targetTable, $targetColumn ?: 'id');
|
|
|
|
return $col;
|
|
}
|
|
|
|
protected function addReference(
|
|
Blueprint $table,
|
|
string $fromColumn,
|
|
string $targetTable,
|
|
?string $targetColumn = null,
|
|
?string $name = null
|
|
): void {
|
|
$table->foreign($fromColumn, $name)
|
|
->references($targetColumn ?: 'id')->on($targetTable)
|
|
->onUpdate('cascade')
|
|
->onDelete('cascade');
|
|
}
|
|
}
|