Open ghost opened 5 years ago
I have posts that have visibility, and users should see all public posts, all posts made by their friends, and all post created by himself, and I only show posts that will not expire in less then hour (time filter) I did it like this
return $query->where([
['visibility_id', $public_visibility_id],
['time', '>', $time],
])->orWhere(function ($query) use ($only_friends_visibility_id, $user_id, $time) {
/* @param $query Builder */
$query->where([
/** Only friends visiblity */
['visibility_id', $only_friends_visibility_id],
['time', '>', $time],
])->whereIn('created_by', function ($q) use ($user_id, $time) {
/** My friends where I was sender of request */
$q->selectRaw(' sender_id FROM friendships WHERE status = ' . Status::ACCEPTED . " AND (`sender_id` = '{$user_id}' OR `recipient_id` = '{$user_id}') AND time > '{$time}'");
})
->orWhereIn('created_by', function ($q) use ($user_id, $time) {
/** My friends where I was recipient of request */
$q->selectRaw(' recipient_id FROM friendships WHERE status = ' . Status::ACCEPTED . " AND (`sender_id` = '{$user_id}' OR `recipient_id` = '{$user_id}') AND time > '{$time}'");
})
->orWhere([
['created_by', $user_id],
['time', '>', $time]
])
->where('time', '>', $time);
});
meetups = posts in this example
You can also chain query builder methods to this, paginate, etc.
I will have to look into for fetching, but for displaying, maybe you can do something with this -
$user->isFriendWith($friend);
and display only if its true. Not ideal but try it out.
Tell me if it works out.
-TT