In your user model you should set 2 relations.
- topicSent (hasMany)
- topicReceived (belongsToMany (i.e. Many-to-Many))
And then, load your 2 relations.
Hi All, I'm suffering. Not (yet a master at eloquent lol).
I have: One table: messageTopics One table: users A belongsToMany relation between both.
In table message_topic, I have a 'sender_id' field. Sender is unique, so it can be directly in this table. Receivers (which are indeed users) can be multiple. So 'belongsToMany' in a table called messageTopics_users, with fields topic_id and receiver_id.
My model: public function receiver() { return $this->belongsToMany(User::class, 'messageTopics_users', 'message_id' ,'receiver_id'); }
For a given "$userId = Auth::user()->id;" I'm trying to get:
Everything is the sale $topics result. So it's two conditions, one in messageTopics table and one in the pivot table.
I tried many things... nothing works....
Any idea?
Thanks FAB
$user = User::with('topicsSent')->with('topicsReceived')->find(Auth::id());
$receivedTopics = $user->topicsReceived;
$sentTopics = $user->sentTopics;
$allTopics = $receivedTopics->merge($sentTopics); //be carefull, I don't know if it will erase some data considered as duplicate.
Please or to participate in this conversation.