N
N
naneri2015-04-11 15:54:44
Laravel
naneri, 2015-04-11 15:54:44

How to get a model in Laravel that has two relationships with specific values?

Currently I am writing a functionality for creating messages on Laravel. The tables are:
Discussions, Participants, Messages. (threads, participants, messages)
The logic is this - one discussion is created between two users, two participants are added to the discussion. Then all messages between participants go to the message table.
When a message is sent, I need to check if there is a discussion between the two participants, if so, add its ID to the message (thread_id).
There was a problem when searching for such a discussion - how do I correctly pull out discussions that have exactly two such users?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gladkovskiy, 2015-04-11
@SMGladkovskiy

As far as I understand, your Discussion has many Participants. When searching for a Discussion, use the whereHas('participants', function($query){...}) method to specify the Participants' IDs or, say, the condition for selecting them by the properties of either the intermediate table or the main table...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question