Skip to content

Commit 4e38e68

Browse files
pascalbaljetcmgmyr
authored andcommitted
BelongsToMany bugfix in Messagable trait
1 parent 37dd9e1 commit 4e38e68

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/Cmgmyr/Messenger/Traits/Messagable.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trait Messagable
1111
*/
1212
public function messages()
1313
{
14-
return $this->hasMany(Config::get('messenger.message_model'));
14+
return $this->hasMany(config('messenger.message_model'));
1515
}
1616

1717
/**
@@ -21,7 +21,7 @@ public function messages()
2121
*/
2222
public function threads()
2323
{
24-
return $this->belongsToMany(Config::get('messenger.thread_model'), 'participants');
24+
return $this->belongsToMany(config('messenger.thread_model'), $this->getParticipantTable(), 'user_id', 'thread_id');
2525
}
2626

2727
/**
@@ -54,23 +54,33 @@ public function threadsWithNewMessages()
5454
* I don't want to include as a dependency for this package...it's overkill. So let's
5555
* exclude this check in the testing environment.
5656
*/
57-
if (getenv('APP_ENV') == 'testing' || !str_contains(\Illuminate\Foundation\Application::VERSION, '5.0')) {
57+
if (getenv('APP_ENV') == 'testing' || !str_contains(\Illuminate\Foundation\Application::VERSION, '5.0'))
58+
{
5859
$participants = $participants->all();
5960
}
6061

61-
if ($participants) {
62+
if ($participants)
63+
{
6264
$threadModelClass = config('messenger.thread_model');
6365
$threadModel = new $threadModelClass;
6466

6567
$threads = $threadModel->whereIn('id', array_keys($participants))->get();
6668

67-
foreach ($threads as $thread) {
68-
if ($thread->updated_at > $participants[$thread->id]) {
69+
foreach ($threads as $thread)
70+
{
71+
if ($thread->updated_at > $participants[$thread->id])
72+
{
6973
$threadsWithNewMessages[] = $thread->id;
7074
}
7175
}
7276
}
7377

7478
return $threadsWithNewMessages;
7579
}
80+
81+
private function getParticipantTable()
82+
{
83+
$participantModel = config('messenger.participant_model');
84+
return (new $participantModel)->getTable();
85+
}
7686
}

0 commit comments

Comments
 (0)