获取同一组中所有用户的数据

时间:2019-05-10 06:26:27

标签: php laravel eloquent relationship

我的代码有效。但我不确定这是否是最佳解决方案。我需要显示用户玩家和/或组用户所属玩家的选项。谢谢您的时间。


$user = $request->user();
$userGroups = $user->groups;
$friendsPlayers = [];

foreach ($userGroups as $group) {

    $groupUsers = $group->users;

    foreach ($groupUsers as $groupUser) {

        if ($groupUser->id !== $user->id) {

            $userPlayer = $groupUser->players;

            foreach ($userPlayer as $player) {

                if (!in_array($player, $friendsPlayers)) {

                    $friendsPlayers[] = $player;
                }
            }
        }
    }
}

1。架构///////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// //////////

 Schema::create('groups', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('info');
        $table->timestamps();
    });

 Schema::create('players', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->index();
        $table->string('name');
        $table->string('url');
        $table->string('type');
        $table->integer('wins');
        $table->integer('lost');
        $table->integer('draws');
        $table->timestamps();
    });

   Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
 Schema::create('group_user', function (Blueprint $table) {

        $table->integer('group_id')->unsigned()->nullable();
        $table->foreign('group_id')->references('id')
              ->on('groups')->onDelete('cascade');

        $table->integer('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')
              ->on('users')->onDelete('cascade');
        $table->timestamps();
    });

2。型号///////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////////////////////////

 class Group extends Model
 {
 protected $fillable = [
    'name', 'info',
 ];
 public function users() {
    return $this->belongsToMany(User::class);
 }
 }


 class Player extends Model
 {   
 protected $guarded = [];


public function user() {
        return $this->belongsTo(User::class);
}
public function leagues() {
    return $this->belongsToMany(League::class)->withPivot('win', 'lost', 
'draw')->withTimestamps();
}


class User extends Authenticatable
{
use Notifiable;


protected $fillable = [
    'name', 'email', 'password',
];


protected $hidden = [
    'password', 'remember_token',
];
public function players() {
    return $this->hasMany(Player::class);
}
public function leagues() {
    return $this->hasMany(League::class);
}
 public function scoreboards() {
    return $this->hasMany(Scoreboard::class);
 }
 public function groups() {
    return $this->belongsToMany(Group::class);
 }
 }

1 个答案:

答案 0 :(得分:0)

这可能是使用Laravel Collection的完美用例。

$result = $user->load('groups.groupUsers.players')
    ->groups
    ->map->groupUsers
    ->collapse()
    ->filter(function ($groupUser) use ($user) {
        return $groupUser->isNot($user);
    })
    ->unique('id')
    ->map->players
    ->collapse()
    ->unique('id');

另一种方法是使用查询来获取结果。

首先,让我们创建一个子查询以获取用户加入的所有组。

$groupsJoinedByUser = Group::whereHas('users', function ($query) use ($user) {
    $query->whereKey($user->id);
});

我们还可以通过以下方式创建该子查询:

$groupsJoinedByUser = Group::select('groups.*')
    ->join('group_user', 'groups.id', '=', 'group_user.group_id')
    ->where('group_user.user_id', $user->id);

现在我们可以创建查询以获取玩家:

$players = Player::select('players.*')
    ->join('users', 'players.user_id', '=', 'users.id')
    ->join('group_user', 'users.id', '=', 'group_user.user_id')
    ->joinSub($groupsJoinsByUser, 'groups_joined_by_user', function($join) {
        $join->on('group_user.group_id', '=', 'groups_joined_by_user.id')
    })
    ->where('users.id', '!=', $user->id);
    ->distinct()
    ->get();