首页 > 解决方案 > 在 laravel 友谊系统中合并两个集合

问题描述

我想在仪表板上显示所有用户,如果我向任何用户发送请求,它的状态应该是“已请求”,或者如果某个用户向我发送请求状态应该是“待定”,否则其他用户的状态应该是“发送请求”。我的控制器是

$users2 = User::with([
    'sentRequests' => function ($query) {
    }

])
->with([
    'recievedRequests' => function ($query) {
    }
])
->where('users.id', Auth::id())
->get();

$users3 = User::where('users.id','!=',Auth::id())
->get();


$users = $users2->merge($users3);

我的模型

 public function sentRequests()
  {
    return $this->belongsToMany('App\User', 'friendships', 'user_id', 'friend_id');
  }

 public function recievedRequests()
  {
    return $this->belongsToMany('App\User', 'friendships', 'friend_id', 'user_id');
  }

但问题出在 $users3 中,它返回了那些在 sentRequests 或 recievedRequests 中已经可见的用户

标签: laravel

解决方案


推荐阅读