Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get unrelated users #94

Open
wagnerdevel opened this issue Mar 18, 2023 · 3 comments
Open

Get unrelated users #94

wagnerdevel opened this issue Mar 18, 2023 · 3 comments

Comments

@wagnerdevel
Copy link

How could I get a list of unrelated users. I need to put together a list of friendship suggestions.

Thanks!

@wagnerdevel
Copy link
Author

I'm currently using it like this:

$users = $user->doesntHaveFriendships()
    ->where('id', '<>', $user->id)
    ->inRandomOrder()
    ->take($limit )
    ->get();

I added in my App\Models\User.php model:

public function invertedFriends()
{
    $friendshipModelName = Interaction::getFriendshipModelName();
    return $this->morphMany($friendshipModelName, 'recipient');
}

public function scopeDoesntHaveFriendships(Builder $builder)
{
    return $builder->whereDoesntHave('friends')
        ->whereDoesntHave('invertedFriends');
}

But I wouldn't want to have additional methods in the model. Any suggestion?

@wagnerdevel
Copy link
Author

The same need happens to list the publications of the user and friends:

$user = auth()->user();

$publications = Publication
    ::where(function (Builder $query) use ($user) {
        $query->whereHas('user.friends', function (Builder $query) {
            $query->where('status', Status::ACCEPTED);
        })->orWhereHas('user.invertedFriends', function (Builder $query) {
            $query->where('status', Status::ACCEPTED);
        })->orWhere('user_id', $user->id);
    })
    ...

It is necessary to use invertedFriends from the User.php model. The ideal would be something like:

$user = auth()->user();

$publications = Publication
    ::where(function (Builder $query) use ($user) {
        $query->whereHas('user.friends', function (Builder $query) {
            $query->where('status', Status::ACCEPTED);
        })->orWhere('user_id', $user->id);
    })
    ...

@Chlemdel
Copy link

Chlemdel commented May 1, 2023

Did you ever figure this out? I'm trying to implement something similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants