【问题标题】:Laravel Sync not working base table not found未找到 Laravel Sync 无法正常工作的基表
【发布时间】: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


【解决方案1】:

如果您使用多对多关系,则必须创建另一个表,其名称为 destination_destination_detail,并带有列:

destination_detail_id
destination_id

都是外键

【讨论】:

    猜你喜欢
    • 2013-07-31
    • 2017-07-29
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    相关资源
    最近更新 更多