Laravel雄辩的关系,3路加入

问题描述 投票:0回答:3

我有3个桌子(这里简化)

用户

id,姓名,电子邮件

球队

id,名字

team_user

team_id,user_id

我想发送一个API查询来返回用户ID所属的所有团队,以及该团队中还有哪些其他成员。我不想仅返回userIds,而是希望用实际的用户数据填充数组,例如名称和电子邮件。

Route::get('/user/{id}/teams/', 'UserController@getTeams');

User.php(型号)

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    public function teams()
    {
        return $this->belongsToMany('App\Team', 'team_user', 'team_id', 'user_id');
    }
}

Team.php(型号)

class Team extends Model
{    
    public function users()
    {
        return $this->belongsToMany('App\User', 'team_user', 'team_id', 'user_id');
    }
}

TeamUser.php(型号)

class TeamMember extends Model
{    
    public function user()
    {
        return $this->hasOne('App\User');
    }

    public function team()
    {
        return $this->hasOne('App\Team');
    }
}

UserController.php

class UserController extends Controller
{
    public function getTeams($userId) {

        return User::find($teamId)->teams()->get();
    }
}

返回:

[
    {
        "id": 6,
        "name": "P12",
        "location": "Newbury",
        "owner": 6,
        "active": 1,
        "created_at": "2017-12-20 10:18:00",
        "updated_at": "2017-12-20 10:18:00",
        "pivot": {
            "team_id": 6,
            "user_id": 6
        }
    },
    {
        "id": 4,
        "name": "fwffewfffffff",
        "location": "ffffffff",
        "owner": 4,
        "active": 1,
        "created_at": "2017-12-19 19:56:27",
        "updated_at": "2017-12-19 19:56:27",
        "pivot": {
            "team_id": 6,
            "user_id": 4
        }
    }
]

但是,我还希望在这两个团队中包含其他用户的列表。使用他们的名字和电子邮件(来自users表),而不仅仅是user_ids。

如果不进行进一步的单独查询,这可能吗?

laravel laravel-5.5
3个回答
1
投票

您将能够热切地加载关系并将它们附加到模型中。

通过使用点符号teams.users,输出将包括附加到id为$userId的用户的所有团队的所有用户。

我添加了findOrFail以确保在找不到用户时它将返回404。

class UserController extends Controller
{
    public function getTeams($userId)       
    {
        return User::with('teams.users')->findOrFail($userId);
    }
}

这将返回用户并附加关系。

如果你想返回团队,你可以这样做:

class UserController extends Controller
{
    public function getTeams($userId)       
    {
        $user = User::with('teams.users')->findOrFail($userId);

        // $user->teams will hold a collection of teams with their users
        return $user->teams;
    }
}

0
投票

例如,尝试使用with()从不同的表中检索字段

class UserController extends Controller {
    public function getTeams($userId) {
         return User::find($userId)->with('teams')->get();
         // return User::find($userId)->teams()->get();
    }
}

如果您想从team_members数据库中选择特定列,可以在with中添加函数,例如

class UserController extends Controller {
    public function getTeams($userId) {
         return User::find($userId)->with(['teams' => function($query) {
             $query->select('id', 'name');
         }])->get();
    }
}

0
投票

现在我已经解决了这个问题

class UserController extends Controller
{
    public function getTeams($userId) {

        $teamWithMembers = [];

        $teams = User::find($userId)->teams()->get();

        foreach($teams as $team) {
            $team->members = Team::find($team->id)->users()->get();
            $teamWithMembers[] = $team;
        }

        return $teamWithMembers;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.