gpt4 book ai didi

php - Eloquent 查找错误的列

转载 作者:搜寻专家 更新时间:2023-10-31 20:37:58 27 4
gpt4 key购买 nike

我的迁移

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com