2015-07-19 59 views
4

如果Auth:user()是當前$ user的朋友,如何檢查?檢查laravel中的朋友

這將被放置在配置文件中,以顯示添加朋友按鈕或掛起的請求等?我嘗試過,與其他方法和條款的關係,但仍然沒有成功。

請大家幫忙,非常感謝!

要恢復這裏是我需要的東西是這樣的:

我需要這樣的東西:

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 

所有這一切,throught laravel口才?

我已經按照如何建立一個雙向的關係,本教程 (Friendship system with Laravel : Many to Many relationship),我有以下幾點:

數據庫表

-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); 
    } 

型號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模型

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

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

我tryed

$friendsAll = Friends::with('User') 
      ->where('user1_id', $user) 
      ->orWhere('user2_id', $user) 
      ->first(); 

回答

0

試試這個:

$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(); 
+0

編輯:使用::輸入給了我一些錯誤,不得不使用查找( 5)作爲用戶示例ID 5 它似乎沒有工作,或者我沒有清楚我的要求,或者我不知道如何使用它,反正,我已經把它放在我的控制器測試,但是,沒有工作,請你指導我?,也謝謝你的時間!記住這是一種雙向的友誼。謝謝 – JCC

+0

你能否在UserController中提供完整的操作代碼? – num8er

+0

是剛添加到代碼中 – JCC