繁体   English   中英

Laravel hasManyThrough 返回空结果

[英]Laravel hasManyThrough returns empty result

我有 3 个表: ChatsMessagesInvoices

在此处输入图像描述

之前,我收到过这样的聊天发票:

//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,您应该将消息表作为 pivot 表!

public function invoices()
{
    return $this->belongsToMany(Invoice::class, 'messages');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM