php - Laravel hasManyThrough 返回空结果
问题描述
我有 3 个表:Chats、Messages、Invoices
之前,我收到过这样的聊天发票:
//ChatModel:
public function invoices()
{
return $this->hasMany(Invoice::class);
}
但是现在我需要获取特定聊天的聊天消息中出现的所有聊天发票。我应该为此使用 hasManyThrough 吗?不幸的是,下一个示例返回一个空结果
//ChatModel:
public function invoices()
{
return $this->hasManyThrough(Invoice::class, Message::class, 'chat_id', 'id');
}
解决方案
由于您的消息表中同时有 chat_id 和 invoice_id,您应该只将消息表作为数据透视表!
public function invoices()
{
return $this->belongsToMany(Invoice::class, 'messages');
}
推荐阅读
- arrays - array[x, y] 是一个有效的 C 表达式吗?
- databricks - Databricks“导入失败并出现错误:服务不可用”
- mysql - 参考父表的 SQL 子查询给出“'where 子句'中的未知列'test_results.id'”
- python - 在 PyQt5 上的标签内添加标签
- primefaces - 选择一个菜单包括搜索primefaces
- python - 在 Altair 中更改误差线厚度
- wpf - 将 x:Name 属性添加到用户控件内的用户控件声明后出现“未定义类型”错误
- python - 您如何让 Finviz 显示特定股票的更多新闻头条?
- r - 如何让餐桌整洁?
- java - doInBackground 不在 Activity onCreate 内运行