使用Laravel的foreignIdFor方法并创建复合唯一键

pdsfdshx  于 2022-12-14  发布在  其他
关注(0)|答案(4)|浏览(125)

按照下面的语法,我可以根据字段namebakery_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']);
});

当我使用“foreignIdFor()“方法而不是“foreignId()“时,有没有办法以编程方式确定列的名称?

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']);
});
ckocjqey

ckocjqey1#

只需捕获列定义并使用该定义:

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']);
});
tp5buhyn

tp5buhyn2#

在laravel 8 foreignIdFor中,可以为第二个参数传递主键

/**
* 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());
}
sirbozc5

sirbozc53#

在laravel 8和更高版本中,您可以将列名指定为第二个参数:

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

然后:

$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

});
对于短代码,您可以链接**-〉unique()**函数

$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();

});

czq61nw1

czq61nw14#

我不明白你的问题,但也许这能帮上忙

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']);
});

相关问题