4

How to check, if Auth:user() is friend of current $user?

This will be placed on the profiles, in order to show add friend buttons or pending requests, etc? I've tryed, relationships with other methods and where clauses, but still no success.

Please help, thank you very much!!

To resume here is I need something like this:

I need something like:

where user1_id = $user->id AND user2_id = Auth::user() AND is_friend = 1 
OR 
where user1_id = Auth::user() AND user2_id = $user->id AND is_friend = 1

All this, throught laravel eloquent?

I have followed this tutorial (Friendship system with Laravel : Many to Many relationship) on how to set up a two way relationship, and I have the following:

Database Tables

-users table-
id
username
age

-friends table-
id ->increments
user1_id ->reference->users.id
user2_id ->reference->users.id
is_accepted - return bool true or false, 1 or 0

UserController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Friend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex($user)
    {

        $relation = User::with('friendsOfMine', 'friendOf', 'profileComments')
            ->where('username', $user);

        $user = User::findOrNew($user)
            ->where('username', $user)
            ->first();

        return view('users.profile')
            ->with('amigos', $relation)
            ->with('user', $user);
    }

Model User.php

//friendship i've started
function friendsOfMine()
{
return $this->belongsToMany('App\User', 'friends', 'user1_id', 'user2_id')
    ->wherePivot('is_friend', '=', 1) // to filter only accepted
    ->withPivot('is_friend'); // or to fetch accepted value
}

// friendship that I was invited to
function friendOf()
{
    return $this->belongsToMany('App\User', 'friends', 'user2_id', 'user1_id')
    ->wherePivot('is_friend', '=', 1)
    ->withPivot('is_friend');
}

// accessor allowing you call $user->friends
    public function getFriendsAttribute()
{
    if ( ! array_key_exists('friends', $this->relations)) $this->loadFriends();

    return $this->getRelation('friends');
}

protected function loadFriends()
{
    if ( ! array_key_exists('friends', $this->relations))
    {
        $friends = $this->mergeFriends();

        $this->setRelation('friends', $friends);
    }
}

protected function mergeFriends()
{
    return $this->friendsOfMine->merge($this->friendOf);
}

Friend.php model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Friend extends Model
{
    protected $table = 'friends';
}

I've tryed

$friendsAll = Friends::with('User')
            ->where('user1_id', $user)
            ->orWhere('user2_id', $user)
            ->first();
Community
  • 1
  • 1
JCC
  • 43
  • 1
  • 9

1 Answers1

-1

Try this:

$User = Auth::user();
$RequestedUser = User::find(Request::input('user_id'));

$Friendship = Friend::with('User')
              ->whereIn('user1_id', [$User->id, $RequestedUser->id])
              ->whereIn('user2_id', [$User->id, $RequestedUser->id])
              ->where('is_friend', '=', 1)
              ->first();
num8er
  • 18,604
  • 3
  • 43
  • 57
  • EDIT: Using ::input gave me some errors had to use find(5) as user example id 5 It seems it didnt worked, or I wasnt clear on my request, or I dont know how to use it, anyways, i've placed it on my controller to test, however, that didnt worked, could you please guide me?, and also Thank you for your time!!! Remember this is a two way friendship. THANK YOU – JCC Jul 20 '15 at 04:33
  • could You provide whole code of action in UserController? – num8er Jul 20 '15 at 09:32