hootlex / laravel-friendships

This package gives Eloquent models the ability to manage their friendships.
MIT License
706 stars 151 forks source link

How To Fetch Posts For only friends #130

Open ghost opened 5 years ago

TechTailor commented 5 years ago

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

kg-bot commented 4 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.