samedi 19 mai 2018

Laravel include user's post in hasManyThrough

I am creating a Social Media application where the user's feed is generated through the hasManyThrough function on the authenticated user, like this: Auth::user()->feed. The function called looks like this:

public function feed() {
    $posts = $this->hasManyThrough(
        'App\Post',
        'App\Follow',
        'follow_by',
        'user_id',
        'id',
        'target_id'
    )->with('user', 'likes', 'comments')
    ->orderBy('id', 'DESC');

    return $posts;
}

This is done since I want to check which users the authenticated user is following, and then find the posts by these people. However, I also want to include the authenticated user's posts in the query. Previously, I have done this separately through $selfPosts = Post::where('user_id', Auth::user()->id)->with('user', 'likes', 'comments')->get(); and then merged the queries using $posts = $selfPosts->merge($followPosts)->sortByDesc('id');.

The problems with merging the queries is many, for example that I cannot use limit nor offset. My question is, how do I include the authenticated user's post in the feed function?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IwO3kC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire