ultimatepos/database/migrations/Support/LegacyForeignKeys.php

58 lines
1.8 KiB
PHP

<?php
namespace Database\Migrations\Support;
use Illuminate\Database\Schema\Blueprint;
/**
* UltimatePOS legacy tables (business, users, products, contacts, pjt_projects)
* use unsigned integer primary keys, not bigint foreignId().
*/
class LegacyForeignKeys
{
public static function business(Blueprint $table, string $column = 'business_id'): void
{
$table->unsignedInteger($column);
$table->foreign($column)->references('id')->on('business')->cascadeOnDelete();
}
public static function user(
Blueprint $table,
string $column,
bool $nullable = true,
string $onDelete = 'null'
): void {
if ($nullable) {
$table->unsignedInteger($column)->nullable();
} else {
$table->unsignedInteger($column);
}
$foreign = $table->foreign($column)->references('id')->on('users');
$onDelete === 'cascade' ? $foreign->cascadeOnDelete() : $foreign->nullOnDelete();
}
public static function product(Blueprint $table, string $column = 'product_id', bool $nullable = true): void
{
if ($nullable) {
$table->unsignedInteger($column)->nullable();
} else {
$table->unsignedInteger($column);
}
$table->foreign($column)->references('id')->on('products')->nullOnDelete();
}
public static function project(Blueprint $table, string $column = 'project_id'): void
{
$table->unsignedInteger($column)->nullable();
$table->foreign($column)->references('id')->on('pjt_projects')->nullOnDelete();
}
public static function contact(Blueprint $table, string $column = 'customer_id'): void
{
$table->unsignedInteger($column)->nullable();
$table->foreign($column)->references('id')->on('contacts')->nullOnDelete();
}
}