2

Following the syntax below, I can easily create a composite unique key based on the fields name and bakery_id:

Schema::create('product_categories', function (Blueprint $table) {
    $table->id();
    $table->foreignId('bakery_id')->constrained();
    $table->string('name', 30);
    $table->boolean('enabled')->default(true);
    $table->timestamps();

    $table->unique(['bakery_id', 'name']);
});

When I use the "foreignIdFor()" method instead of "foreignId()", is there a way to programmatically determine the name of the column?

Schema::create('product_categories', function (Blueprint $table) {
    $table->id();
    $table->foreignIdFor(Bakery::class)->constrained();
    $table->string('name', 30);
    $table->boolean('enabled')->default(true);
    $table->timestamps();

    $table->unique(['???', 'name']);
});
miken32
  • 42,008
  • 16
  • 111
  • 154
nekiala
  • 450
  • 9
  • 21

4 Answers4

3

Just need to capture the column definition and use that:

Schema::create('product_categories', function (Blueprint $table) {
    $table->id();
    $colDef = $table->foreignIdFor(Bakery::class)->constrained();
    $table->string('name', 30);
    $table->boolean('enabled')->default(true);
    $table->timestamps();

    $table->unique([$colDef->name, 'name']);
});
Shaun
  • 66
  • 5
  • 3
    Can't you just use `$colDef->name`? Also, not worth a separate answer, but `(new Bakery)->getForeignKey()` will also work. – miken32 Dec 13 '21 at 23:24
  • @miken32 Cleaned up the attribute – Shaun Dec 14 '21 at 04:32
  • How does the `down()` method look like? As for the dropping indexes ... – Pathros Aug 02 '22 at 17:17
  • When tried the above answer. SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'foreign' doesn't exist in table (SQL: alter table `table_name` add unique `index_name`(`foreign`)); As suggested by @miken32 (new Bakery)->getForeignKey() Worked for me. – Lokesh Oct 21 '22 at 09:41
2

In laravel 8 foreignIdFor, you can pass primary key for the second argument

/**
* Create a foreign ID column for the given model.
*
* @param  \Illuminate\Database\Eloquent\Model|string  $model
* @param  string|null  $column
* @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
*/
public function foreignIdFor($model, $column = null)
{
    if (is_string($model)) {
        $model = new $model;
    }

    return $model->getKeyType() === 'int' && $model->getIncrementing()
                ? $this->foreignId($column ?: $model->getForeignKey())
                : $this->foreignUuid($column ?: $model->getForeignKey());
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
1

in laravel 8 and above you can specify the column name as a second paramete:

$table->foreignIdFor(Bakery::class,'bakery_id')->constrained();

and then:

$table->unique([bakery_id, 'name']);

Schema::create('product_categories', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Bakery::class,'bakery_id')->constrained();//pass the column name as second param
$table->string('name', 30);
$table->boolean('enabled')->default(true);
$table->timestamps();

$table->unique(['bakery_id', 'name']);//column name

});

for short code you can chain the ->unique() function

$table->unique([bakery_id, 'name']);

Schema::create('product_categories', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Bakery::class,'bakery_id')->unique()->constrained();//pass the column name as second param and chain the unique function
$table->string('name', 30)->unique();//chain the unique function
$table->boolean('enabled')->default(true);
$table->timestamps();

});

-1

I don't get your question but maybe this might help

Schema::create('product_categories', function (Blueprint $table) {
   $table->id();
   // $table->foreignIdFor(Bakery::class)->constrained();
   $table->unsignedInteger('bakery_id')->nullable();
   $table->foreign('bakery_id')->references('id')->on('bake')->onDelete('cascade');
   $table->string('name', 30);
   $table->boolean('enabled')->default(true);
   $table->timestamps();

   $table->unique(['bakery_id', 'name']);
});
Mostafa Hana
  • 115
  • 2
  • 8