Skip to content

Commit 38c8f45

Browse files
committed
removed get() and latest() references in model scopes
1 parent 9389731 commit 38c8f45

File tree

4 files changed

+10
-14
lines changed

4 files changed

+10
-14
lines changed

src/Cmgmyr/Messenger/Models/Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ public function participants()
7272
*/
7373
public function recipients()
7474
{
75-
return $this->participants()->where('user_id', '!=', $this->user_id)->get();
75+
return $this->participants()->where('user_id', '!=', $this->user_id);
7676
}
7777
}

src/Cmgmyr/Messenger/Models/Thread.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function participants()
6767
*/
6868
public static function getAllLatest()
6969
{
70-
return self::latest('updated_at')->get();
70+
return self::latest('updated_at');
7171
}
7272

7373
/**
@@ -99,9 +99,7 @@ public function scopeForUser($query, $userId)
9999
return $query->join('participants', 'threads.id', '=', 'participants.thread_id')
100100
->where('participants.user_id', $userId)
101101
->where('participants.deleted_at', null)
102-
->select('threads.*')
103-
->latest('updated_at')
104-
->get();
102+
->select('threads.*');
105103
}
106104

107105
/**
@@ -120,9 +118,7 @@ public function scopeForUserWithNewMessages($query, $userId)
120118
$query->where('threads.updated_at', '>', $this->getConnection()->raw($this->getConnection()->getTablePrefix() . 'participants.last_read'))
121119
->orWhereNull('participants.last_read');
122120
})
123-
->select('threads.*')
124-
->latest('updated_at')
125-
->get();
121+
->select('threads.*');
126122
}
127123

128124
/**

src/Cmgmyr/Messenger/examples/MessagesController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public function index()
3232
$currentUserId = Auth::user()->id;
3333

3434
// All threads, ignore deleted/archived participants
35-
$threads = Thread::getAllLatest();
35+
$threads = Thread::getAllLatest()->get();
3636

3737
// All threads that user is participating in
38-
// $threads = Thread::forUser($currentUserId);
38+
// $threads = Thread::forUser($currentUserId)->latest('updated_at')->get();
3939

4040
// All threads that user is participating in, with new messages
41-
// $threads = Thread::forUserWithNewMessages($currentUserId);
41+
// $threads = Thread::forUserWithNewMessages($currentUserId)->latest('updated_at')->get();
4242

4343
return view('messenger.index', compact('threads', 'currentUserId'));
4444
}

tests/EloquentThreadTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function it_should_return_all_threads()
6363
$this->faktory->create('thread', ['id' => ($index + 1)]);
6464
}
6565

66-
$threads = Thread::getAllLatest();
66+
$threads = Thread::getAllLatest()->get();
6767

6868
$this->assertCount($threadCount, $threads);
6969
}
@@ -100,7 +100,7 @@ public function it_should_get_all_threads_for_a_user()
100100
$participant_2 = $this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread2->id]);
101101
$thread2->participants()->saveMany([$participant_2]);
102102

103-
$threads = Thread::forUser($userId);
103+
$threads = Thread::forUser($userId)->get();
104104
$this->assertCount(2, $threads);
105105
}
106106

@@ -117,7 +117,7 @@ public function it_should_get_all_threads_for_a_user_with_new_messages()
117117
$participant_2 = $this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread2->id, 'last_read' => Carbon::yesterday()]);
118118
$thread2->participants()->saveMany([$participant_2]);
119119

120-
$threads = Thread::forUserWithNewMessages($userId);
120+
$threads = Thread::forUserWithNewMessages($userId)->get();
121121
$this->assertCount(1, $threads);
122122
}
123123

0 commit comments

Comments
 (0)