gpt4 book ai didi

laravel-4 - 模式生成器 Laravel 迁移在两列上是唯一的

转载 作者:行者123 更新时间:2023-11-29 15:42:35 24 4
gpt4 key购买 nike

如何在两列上设置唯一约束?

class MyModel extends Migration {
public function up()
{
Schema::create('storage_trackers', function(Blueprint $table) {
$table->increments('id');
$table->string('mytext');
$table->unsignedInteger('user_id');
$table->engine = 'InnoDB';
$table->unique('mytext', 'user_id');
});
}
}

MyMode::create(array('mytext' => 'test', 'user_id' => 1);
// this fails??
MyMode::create(array('mytext' => 'test', 'user_id' => 2);

最佳答案

第二个参数是手动设置唯一索引的名称。使用数组作为第一个参数来创建跨多个列的唯一键。

$table->unique(array('mytext', 'user_id'));

或者(更整洁一点)

$table->unique(['mytext', 'user_id']);

关于laravel-4 - 模式生成器 Laravel 迁移在两列上是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57447979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com