微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

雄辩寻找错误的列

我的迁移

Schema::create('tickets', function(Blueprint $table)
    {
        $table->increments('id');
        $table->text('subject');
        $table->text('body');
        $table->integer('author_id')->unsigned();
        $table->timestamps();

    });

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

关系

public function createdTickets()
{
    return $this->hasMany('App\Ticket');
}


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

和存储方法

    public function store(TicketRequest $request)
{
    $ticket = new Ticket($request->all());
    Auth::user()->createdTickets()->save($ticket);

    return redirect('tickets');
}

当我尝试保存新票证时,我收到此错误消息

QueryException in Connection.PHP line 624:
sqlSTATE[HY000]: General error: 1 table tickets has no column named user_id (sql: insert into “tickets” (“subject”, “body”, “user_id”, “updated_at”, “created_at”)

除关系外,我还需要在哪里指定FK?

解决方法:

您需要在两种关系方法中指定所需的列名.

public function createdTickets()
{
    return $this->hasMany('App\Ticket', 'author_id');
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。