Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

Friends is a pivot table code is

public function friends() {
    return $this->belongsToMany('Trinity\Users\User', 'friends', 'user_id', 'friend_id');
}
Last updated 2 years ago.
0

Have you ever tried this?

$friend = User::whereHas('friends', function($query) use ($id, $user)
{
   $query->where('friend_id', '=', $id)->where('user_id', '=', $user);
})->count();
Last updated 2 years ago.
0

But there is an option that, user_id is the visiting user id or the other way around.

Last updated 2 years ago.
0

I see your point.

Make this change:

$query->where('friend_id', '=', $id)->where('user_id', '=', $user)->orWhere('friend_id', '=', $user)->where('user_id', '=', $id);
Last updated 2 years ago.
0

I got it working with this

$friend = $this->friends()->where('friend_id', '=', $id)->where('user_id', '=', $user)->orWhere('friend_id', '=', $user)->where('user_id', '=', $id)->first();
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

eriktisme eriktisme Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.