summaryrefslogtreecommitdiff
path: root/db/migrations/Reference.php
blob: 719bee76c11bd9b2f92e6467a090ed8390aab551 (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
<?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)
            ->onUpdate('cascade')
            ->onDelete('cascade');
    }
}