public function threads($user, $offset, $take)
{
$conv = new Conversation();
$conv->authUser = $user;
$msgThread = $conv->with(['messages' => function ($q) use($user) {
return $q->where(function ($q) use($user) {
$q->where('user_id', $user)->where('deleted_from_sender', 0);
})->orWhere(function ($q) use($user) {
$q->where('user_id', '!=', $user);
$q->where('deleted_from_receiver', 0);
})->latest();
}, 'userone', 'usertwo'])->where('user_one', $user)->orWhere('user_two', $user)->offset($offset)->take($take)->get();
$threads = [];
foreach ($msgThread as $thread) {
$collection = (object) null;
$conversationWith = $thread->userone->id == $user ? $thread->usertwo : $thread->userone;
$collection->thread = $thread->messages->first();
$collection->withUser = $conversationWith;
$threads[] = $collection;
}
return collect($threads);
}