blob: d0550686adafe802a11b296ce1a289d03a540ab5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
<?php
namespace Engelsystem\Migrations;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ColumnDefinition;
use Illuminate\Support\Str;
trait Reference
{
/**
* @param Blueprint $table
* @param bool $setPrimary
*/
protected function referencesUser(Blueprint $table, bool $setPrimary = false)
{
$this->references($table, 'users', null, $setPrimary);
}
/**
* @param Blueprint $table
* @param string $targetTable
* @param string|null $fromColumn
* @param bool $setPrimary
* @return ColumnDefinition
*/
protected function references(
Blueprint $table,
string $targetTable,
?string $fromColumn = null,
bool $setPrimary = false
): ColumnDefinition {
$fromColumn = $fromColumn ?? Str::singular($targetTable) . '_id';
$col = $table->unsignedInteger($fromColumn);
if ($setPrimary) {
$table->primary($fromColumn);
}
$table->foreign($fromColumn)
->references('id')->on($targetTable)
->onUpdate('cascade')
->onDelete('cascade');
return $col;
}
}
|