我在laravel v4.2项目中工作,现在我不明白我如何重新解决这个问题。 我有一个用户数组和登录用户ID现在我想要检查登录用户是否跟随数组中的任何用户,要么是任何单个数组用户跟随登录用户。然后其中一个跟随并且两个数组彼此跟随然后它返回true。但我希望通过laravel relashionship这个问题。 下面是我编码的代码
$login_id = 2;
$users_arr = Array
(
[0] => 87
[1] => 9595
[2] => 8625
[3] => 18368
[4] => 18363
[5] => 18371
)
class User extends Eloquent {
protected $table = 'users';
protected $primaryKey = 'id';
protected $guarded = array();
protected $hidden = array();
public function mutual_friends() {
return $this->belongsToMany('User', 'friends', 'follower_id', 'following_id');
}
public static function get_users_infomation_by_ids($login_id, $users_arr = array()) {
$users = User::where(function($sql) use($login_id, $users_arr) {
$sql->whereIn('id', $users_arr);
})->with(['mutual_friends'=>function($sql) use($login_id){
$sql->where('follower_id','=',$login_id);
$sql->orWhere('following_id','=',$login_id);
}])->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));
return (!empty($users) && count($users) > 0) ? $users->toArray() : array();
}
}
朋友表包含列{id,follower_id,following_id}
现在我希望任何一个条件都为真,然后函数返回true并显示用户信息。
输出
<pre>Array
(
[0] => Array
(
[id] => 87
[username] => rizwan.saleem
[full_name] => Rizwan Saleem
[is_live] => 0
[message_privacy] => 0
[picture] => vmiboiagcropped-5589294861513766548.jpg
[mutual_friends] => Array
(
[0] => Array
(
[id] => 2
[username] => yahoo
[pivot] => Array
(
[following_id] => 87
[follower_id] => 2
)
)
)
)
[1] => Array
(
[id] => 8625
[username] => hassan_shahid
[full_name] => Hassan Shahids
[is_live] => 0
[message_privacy] => 0
[picture] => profile_default.jpg
[mutual_friends] => Array
(
[0] => Array
(
[id] => 2
[username] => yahoo
[pivot] => Array
(
[following_id] => 8625
[follower_id] => 2
)
)
)
)
[2] => Array
(
[id] => 9595
[username] => majo
[full_name] => Majo mike
[is_live] => 1
[message_privacy] => 0
[picture] => rwls1p2jcropped507977901504873847.jpg
[mutual_friends] => Array
(
[0] => Array
(
[id] => 2
[username] => yahoo
[pivot] => Array
(
[following_id] => 9595
[follower_id] => 2
)
)
)
)
[3] => Array
(
[id] => 18363
[username] => asif_ilsa
[full_name] => Asif Ilsa
[is_live] => 1
[message_privacy] => 0
[picture] => ydi1dmtdupload-image1512648978.jpg
[mutual_friends] => Array
(
)
)
[4] => Array
(
[id] => 18368
[username] => ali.abbas
[full_name] => Ali Abbas 1
[is_live] => 1
[message_privacy] => 0
[picture] => dsczoljfcropped4868760551505544665.jpg
[mutual_friends] => Array
(
[0] => Array
(
[id] => 2
[username] => yahoo
[pivot] => Array
(
[following_id] => 18368
[follower_id] => 2
)
)
)
)
[5] => Array
(
[id] => 18371
[username] => maju
[full_name] => Mani Dude
[is_live] => 0
[message_privacy] => 2
[picture] => ndtcihu7upload-image1503915762.jpg
[mutual_friends] => Array
(
[0] => Array
(
[id] => 2
[username] => yahoo
[pivot] => Array
(
[following_id] => 18371
[follower_id] => 2
)
)
)
)
)
在上面的outpu id-&gt; 18363没有相互记录,这只是因为这个 用户即使是跟随此用户的登录用户也不会关注登录用户。
答案 0 :(得分:1)
$users = User::whereIn('id', $users_arr)
->with('mutual_friends')
->where(function ($query) use ($login_id){
$query->whereRaw('users.id=friends.follower_id OR users.id=friends.following_id')->where(function($q) use ($login_id){
$q->where('follower_id',$login_id);
});
})
->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));