【发布时间】:2020-08-05 03:42:01
【问题描述】:
我有这个错误
SQLSTATE[42S02]: Base table or view not found: 1146 `Table 'skin_db.destination_destination_detail'` doesn't exist `(SQL: select * from `destination_destination_detail` where `destination_id` = 8)`
实际上我只有destinations 和destination_details 表。但错误是在寻找destination_destination_detail 表?
我真的不明白为什么。
目标模型
public function details(){
return $this->belongsToMany('App\Destination_detail');
}
控制器
$destination = Destination::find($req->destination_id);
$destination->details()->sync($req->provinces);
【问题讨论】:
-
如果您只有 2 个表,则您没有
BelongsToMany关系;这需要一个数据透视表... Laravel 约定假设数据透视表名称为模型 A + 模型 B,这将是目标 + 目标详细信息 (destination_destination_detail),如果您想为数据透视表使用不同的表名,则必须使用belongsToMany的其他参数来定义它 -
与您的问题无关,但模型名称中的下划线是禁忌,大写 DestinationDetail
-
@lagbox,我现在明白了!.. 非常感谢。
标签: laravel eloquent relational-database laravel-7