php - Eloquent 查找错误的列

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

当我尝试保存一张新票时,我收到这个错误

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

关于php - Eloquent 查找错误的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558957/

相关文章:

PHP 日期池中的最大天数

php - 如何从 laravel5 中的中间件抛出禁止的异常?

php - 拉拉维尔 : Destroy and detach method

php - 如何根据出生日期获得本月退休金

php - php数组循环值和键

php - CakePHP 图标

php - Laravel - 从多维数组中删除具有 NULL 值的元素

php - Laravel 错误 - 找不到 Controller ,但它在那里

database - 从两列具有相同值的表中选择

php - 预编译语句查询不返回任何结果