, , 5 . , , .
$query = Post::query();
, , $me
.
, ,
$followingUserIds = $me
->following()
->where('followable_type', User::class)
->lists('followable_id');
,
$myFieldIds = $me->schoolables()->lists('field_id');
$sharedFieldUserIds = Schoolable::whereIn('field_id', $myFieldIds)->lists('user_id');
, ,
$mySchoolIds = $me->schoolables()->lists('school_id');
$sharedSchoolUserIds = Schoolable::whereIn('school_id', $mySchoolIds)->lists('user_id');
:
$query->where(function($inner) use ($me) {
$inner->where('posts.author_type', User::class);
$inner->where('posts.author_id', $me->id);
});
, , ()
$query->orWhere(function($inner) use ($followingUserIds) {
$inner->where('posts.author_type', User::class);
$inner->whereIn('posts.author_id', $followingUserIds);
});
, , ,
: ->whereHas
, , .
$query->orWhereHas('comments', function($subquery) use ($followingUserIds) {
$subquery->where('comments.author_type', User::class);
$subquery->whereIn('comments.author_id', $followingUserIds);
});
.
,
$query->orWhere(function($inner) use ($sharedFieldUserIds) {
$inner->where('posts.author_type', User::class);
$inner->whereIn('posts.author_id', $sharedFieldUserIds);
});
,
, poeple school_id
$query->orWhere(function($inner) use ($sharedSchoolUserIds) {
$inner->where('posts.author_type', User::class);
$inner->whereIn('posts.author_id', $sharedSchoolUserIds);
});
,
$posts = $query->get();
, , - . , , Post
Comments
. , , , -, , Post::authoredBySomeoneFollowedByUser($me)
, , -, .