summaryrefslogtreecommitdiff
path: root/db/migrations/Reference.php
blob: 4c35b59b816fadd534c3b12a62296122d5fcb155 (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
<?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');
    }
}