<?php


namespace App\Helpers\DatabaseSchema;


use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
use Illuminate\Database\Schema\ForeignKeyDefinition;
use Illuminate\Support\Str;

class CustomForeignIdColumnDefinition extends ForeignIdColumnDefinition {

    public function __construct(Blueprint $blueprint, $attributes = []){
        parent::__construct($blueprint, $attributes);
    }

    /**
     * Create a foreign key constraint on this column referencing the "id" column of the conventionally related table.
     *
     * @param  string|null  $table
     * @param  string  $column
     * @param  string|null  $indexName
     * @return ForeignKeyDefinition
     */
    public function constrained($table = null, $column = 'id',$indexName = null){
        return $this->references($column,$indexName)
                    ->on($table ?? Str::of($this->name)
                    ->beforeLast('_'.$column)->plural());
    }

    /**
     * Specify which column this foreign ID references on another table.
     *
     * @param  string  $column
     * @param  string|null  $indexName
     * @return ForeignKeyDefinition
     */
    public function references($column,$indexName = null){
        return $this->blueprint->foreign($this->name,$indexName)->references($column);
    }
}
