Laravel 5.2外键失败

时间:2016-08-02 21:01:16

标签: php mysql laravel

我正在尝试在posts表中添加一个外键来引用users表中的id。以下是我的迁移,用户表是默认的laravel,所以时间戳是从2014年11月开始的,但帖子是我创建的。非常感谢任何帮助。

非常感谢:)

用户迁移         

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name')->unique();
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

发布迁移

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('title');
            $table->string('section');
            $table->string('body');
            $table->date('post_created_date');
            $table->integer('author_id')->unsigned()->after('id');
            $table->foreign('author_id')->references('id')->on('users');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('posts');
    }
}

每次迁移我都会收到以下错误:

ERROR

2 个答案:

答案 0 :(得分:3)

如果您要创建表格,则不希望使用after,因为它会产生语法错误。只需将列放在您想要的位置即可。我相信after只适用于alter table语句。

    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');  // Unsigned is assumed for increments so I removed that as well
        $table->integer('author_id')->unsigned();
        $table->string('title');
        $table->string('section');
        $table->string('body');
        $table->date('post_created_date');
        $table->timestamps();
        $table->foreign('author_id')->references('id')->on('users');
    });

然后在您的模型中,您将设置与users表的关系,就像这样...

public function author()
{
    return $this->belongsTo(User::class, 'author_id');
}

您可以根据需要随意重命名该功能,我通常更喜欢将其命名为与外键相匹配的内容。

然后你就像$post->author->name一样使用它。

答案 1 :(得分:2)

在您的CreatePostsTable类中,您可以尝试这个吗?

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('author_id')->unsigned();
        $table->string('title');
        $table->string('section');
        $table->string('body');
        $table->date('post_created_date');
        $table->timestamps();
    });

   Schema::table('posts', function($table) {
       $table->foreign('author_id')->references('id')->on('users');
   });
}