Eloquent寻找错误的专栏

时间:2015-05-31 15:07:42

标签: php laravel eloquent

我的迁移

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');
}

当我尝试保存新票时,我发现了这个错误

  

Connection.php第624行中的QueryException:   SQLSTATE [HY000]:常规错误:1个表票证没有名为user_id的列(SQL:插入“票证”(“subject”,“body”,“user_id”,“updated_at”,“created_at”)

除了关系之外我还需要指定FK吗?

1 个答案:

答案 0 :(得分:3)

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

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