Given the following models
Conversation: id, user_id,
Message: id, conversation_id, author_id, created_at
I wanted to query all conversations who have messages between $start
and $end
, so i did this:
$filterMessages = function($query) use ($start, $end) {
$query->whereBetween("created_at", [$start, $end]);
};
$convs = Conversation::whereHas("messages", $filterMessages)
->with(["messages" => $filterMessages]);
And it works. Now i need to add a condition: the message.user_id must match his conversation's user_id (both in whereHas and with). So i changed my callback to this:
$filterMessages = function($query) use ($start, $end) {
$query->whereBetween("created_at", [$start, $end]);
$query->where("conversations.user_id", "author_id");
};
But it does not work. How could i do that?
Thanks