Laravel仅提取多对多关系中的数据透视列

时间:2018-05-31 09:31:00

标签: php laravel laravel-5 orm relational-database

我有一个User模型,它通过数据透视模型SectionUserTrainingSection模型相关联。我有一个数据透视表,用于存储名为section_user的两个表的外键。

我有2个额外的数据透视表列,名为completedcompleted_date

我遇到的问题是,当我获取数据时,它会返回User模型中的所有列以及其他数据透视列。

    class Section extends Model
    {
        public $table = "sections";

        public $fillable = [
            'id',
            'name',
            'description',
            'parent',
            'position',
            'completion_percentage'
        ];

        public function users()
        {
            return $this->belongsToMany(User::class, 'section_user')->using('App\Models\UserTrainingSection')->withPivot('completed', 'completed_date');
        }
    }

在我的API服务中,我获取了这样的数据:

Section::with(['users' => function ($q) {
                $q->where('users.id', Auth::user()->id);
            }])->first();

如何仅返回数据透视表列并从用户表中排除列?

目前它返回的内容如下:

"sections": [
                {
                    "id": 2,
                    "name": "Subsection 1 training",
                    "description": null,
                    "parent": 1,
                    "position": 2,
                    "completion_percentage": null,
                    "created_at": "2018-05-04 09:54:09",
                    "updated_at": "2018-05-11 09:14:59",
                    "users": [
                        {
                            "id": 1,
                            "name": "Test",
                            "email": "test@test.com",
                            "created_at": "12-04-2018 14:51:42",
                            "updated_at": "2018-04-19 14:14:36",
                            "pivot": {
                                "section_id": 2,
                                "user_id": 1,
                                "completed": 1,
                                "completed_date": "31/05/2018",
                                "expires": "31/05/2019"
                            },
                        }
                    ]
                }
            ]

我想要回归的是:

"sections": [
                    {
                        "id": 2,
                        "name": "Subsection 1 training",
                        "description": null,
                        "parent": 1,
                        "position": 2,
                        "completion_percentage": null,
                        "created_at": "2018-05-04 09:54:09",
                        "updated_at": "2018-05-11 09:14:59",
                        "users": [
                            {
                                "pivot": {
                                    "section_id": 2,
                                    "user_id": 1,
                                    "completed": 1,
                                    "completed_date": "31/05/2018",
                                    "expires": "31/05/2019"
                                }
                            }
                        ]
                    }
                ]

所以我基本上摆脱了我不想要的用户数据,只返回数据透视数据。

2 个答案:

答案 0 :(得分:2)

根据您的预期输出,我猜您只需要来自结点模型的数据,如果是这种情况,我建议您定义SectionUserTrainingSection的直接映射

class Section extends Model
{

    public function training_users()
    {
        return $this->hasMany('App\Models\UserTrainingSection', 'section_id');
    }
    public function users()
    {
        return $this->belongsToMany(User::class, 'section_user')->using('App\Models\UserTrainingSection')->withPivot('completed', 'completed_date');
    }
}

在查询中,您只需执行

Section::with('training_users')->first();

答案 1 :(得分:0)

with语句中,您按照定义提取所有User数据。 您可以添加select语句以仅获取所需的结果。您可以在模型中的关系中添加,也可以在API中访问模型。

示例1:

Section::with(['users' => function ($q) {
    $q->where('users.id', Auth::user()->id)->select('completed', 'completed_date');
}])->first();

示例2:

public function users()
    {
        return $this->belongsToMany(User::class, 'section_user')->using('App\Models\UserTrainingSection')->withPivot('completed', 'completed_date')->select('completed', 'completed_date');
    }

(未经测试)

See the documentation