【问题标题】:Laravel 5.2 Foreign Key failsLaravel 5.2 外键失败
【发布时间】:2016-08-02 21:01:16
【问题描述】:

我正在尝试在我的帖子表中添加一个外键来引用用户表中的 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');
    }
}

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

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    如果你正在创建一个表,你不想使用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-&gt;author-&gt;name一样使用它。

    【讨论】:

    • 如果您不介意,还有一个问题,当我将用户表回显到页面上时,如何从用户表访问用户。我正在尝试 {{ $post->author_id()->name }} 但这会引发错误。这是我在模型中做的事情吗?
    • 非常感谢@user3158900 的帮助
    【解决方案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');
       });
    }
    

    【讨论】:

    • 如果您不介意,还有一个问题,当我将用户表回显到页面上时,如何从用户表访问用户。我正在尝试 {{ $post->author_id()->name }} 但这会引发错误。这是我在模型中做的事情吗?
    • 您必须在模型laravel.com/docs/5.2/eloquent-relationships 中定义关系。然后你就可以打电话给{{ $post-&gt;author-&gt;name }} :)