关系过滤器与雄辩的回调



给定以下模型

对话:ID,USER_ID,

消息:ID,Converty_id,rutire_id,create_at

我想查询所有在$start$end之间有消息的对话,所以我做到了:

$filterMessages = function($query) use ($start, $end) {
    $query->whereBetween("created_at", [$start, $end]);
};
$convs = Conversation::whereHas("messages", $filterMessages)
->with(["messages"  => $filterMessages]);

它有效。现在,我需要添加一个条件:Message.user_id必须匹配他的对话的User_id(在HORE和HAS)。所以我对此更改了回调:

$filterMessages = function($query) use ($start, $end) {
    $query->whereBetween("created_at", [$start, $end]);
    $query->where("conversations.user_id", "author_id");
};

但它行不通。我该怎么办?

谢谢

尝试此

Conversation Model定义关系

public function userMessages()
{
   return $this->hasMany(Message::class, 'author_id', 'user_id'); //your relationship based on data structure
}
$convs = Conversation::whereHas('userMessages',function($query) use ($start, $end) {
                           $query->whereBetween("created_at", [$start, $end]);
                       })
                      ->with("userMessages")
                      ->get();

相关内容

  • 没有找到相关文章

最新更新