Laravel关系与“ GROUP_CONCAT”值

时间:2018-09-10 11:24:45

标签: php laravel eloquent relationship

我有一个包含多个日期的时间表,我有这种关系

$report = CaregiverSchedule::select(
        DB::raw('GROUP_CONCAT(id) as ids'),
        DB::raw('GROUP_CONCAT(client_id) as client_ids'),
        DB::raw('GROUP_CONCAT(caregiver_id) as caregiver_id'),
        'agency_id', 
        'valid_from', 
        'valid_to',
        DB::raw('COUNT(DATE(valid_from)) AS conflict_count'),
        DB::raw("IF(COUNT(DATE(valid_from)) > 1, TRUE, FALSE) as conflicts"),
        DB::raw('GROUP_CONCAT(valid_from) as dates')
    )->groupBy(DB::raw('DATE(valid_from)'))
    ->having('conflicts',true)
    ->where('client_id',4);

它返回的记录是:

[
    {
        "ids": "26,4",
        "client_ids": "4,4",
        "caregiver_id": "12,3",
        "agency_id": 2,
        "valid_from": "2018-07-10 09:00:00",
        "valid_to": "2018-07-10 17:00:00",
        "conflict_count": 2,
        "conflicts": 1,
        "dates": "2018-07-10 09:00:00,2018-07-10 09:00:00"
    },
    {
        "ids": "5,27",
        "client_ids": "4,4",
        "caregiver_id": "3,12",
        "agency_id": 2,
        "valid_from": "2018-07-11 09:00:00",
        "valid_to": "2018-07-11 17:00:00",
        "conflict_count": 2,
        "conflicts": 1,
        "dates": "2018-07-11 09:00:00,2018-07-11 09:00:00"
    }
]

请注意caregiver_id(例如12,3)是否有可能在雄辩的Laravel中将caregiver_id返回具有“ hasMany”关系的用户对象?

例如:

{
    "ids": "40,18",
    "client_ids": "4,4",
    "caregiver_id": "12,3",
    "caregivers":[
        {
            id: 12,
            name: "john"
        },
        {
            id: 3,
            name: "doe"
        }
    ]
    "agency_id": 2,
    "valid_from": "2018-07-24 09:00:00",
    "valid_to": "2018-07-24 17:00:00",
    "conflict_count": 2,
    "conflicts": 1,
    "dates": "2018-07-24 09:00:00,2018-07-24 09:00:00"
}

0 个答案:

没有答案