Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何通过外键laravel_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 如何通过外键laravel

Php 如何通过外键laravel,php,laravel,laravel-5,Php,Laravel,Laravel 5,我试图传递外键,但遇到此错误基本表或视图已存在:1050表“收藏夹”已存在“ 如何传递所有外键和普通id,如$table->biginger('user_id')->unsigned(); 桌子 public function up() { Schema::create('favorites', function (Blueprint $table) { $table->Increments('id'); $table->bigIntege

我试图传递外键,但遇到此错误
基本表或视图已存在:1050表“收藏夹”已存在“
如何传递所有外键和普通id,如
$table->biginger('user_id')->unsigned();

桌子

 public function up()
{

    Schema::create('favorites', function (Blueprint $table) {
        $table->Increments('id');
        $table->bigInteger('user_id')->unsigned();
        $table->bigInteger('product_id')->unsigned();
        $table->timestamps();

    });

    Schema::create('favorites', function (Blueprint $table){

        $table->bigInteger('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users');

        $table->bigInteger('product_id')->unsigned()->index();
        $table->foreign('product_id')->references('id')->on('products');
    });

}

public function down()
{
    Schema::dropIfExists('favorites');
}

如果要迁移表两次,请在同一次迁移中传递外键

public function up()
{
Schema::create('favorites',函数(Blueprint$表){
$table->bigIncrements('id');
$table->biginger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->biginger('product_id')->unsigned();
$table->foreign('product_id')->引用('id')->关于('products');
$table->timestamps();
});
}
公共职能部门
{
Schema::dropIfExists('favorites');
}

希望这有帮助

其他答案都是正确的。如果你在做之前错过了所需的任务,请尝试一下这个

1) 删除或删除已迁移的表

2) 在迁移表中,删除包含名称“收藏夹”的记录

3) 将该代码复制并粘贴到“up”函数中

Schema::create('favorites', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('user_id')->unsigned();
    $table->bigInteger('product_id')->unsigned();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users');
    $table->foreign('product_id')->references('id')->on('products');

});
4) 再次运行此命令

php artisan migrate

将第二个块移到第一个块的内部。同一个表不能有两个
create
块。请将第二个
Schema::create
更改为
Schema::table
,因为此时您不是在创建新表,而是在修改表