我尝试从数据库中获取事件$eventWithDatas = Event::with(['persons' => function($query) { $query->select('persons.id', 'name', 'title', 'status') ->where('date_of_death', null); }, 'persons.health_items' => function($query) { $query->select('id', 'name', 'date_in', 'date_end', 'person_id'); }])->get();我想过滤我的关系persons.health_items。理想的情况是拥有这样的东西:$eventWithDatas = Event::with(['persons' => function($query) { $query->select('persons.id', 'name', 'title', 'status') ->where('date_of_death', null); }, 'persons.health_items' => function($query) use ($eventWithDatas) { $query->select('id', 'name', 'date_in', 'date_end', 'person_id') ->where('date_end', $eventWithDatas->end); }])->get();$eventWithDatas应该是我的查询的第一部分 ( Event::get)结果示例:[{ / event collection (contain just 1 item for example) "title": "vaccines", "start": "2020-07-24 01:00:00", "end": "2020-07-24 01:00:00", // <----------- I WANT TO USE THIS DATA FOR FILTER persons.health_items.date_end (see bottom) "persons": [ "id": 1, "name": "Jeremy", ... "health_items": [ { "id": 14, "name": "my health item", "date_end": // <-------------------- DATA TO FILTER (see top) (2020-07-24 01:00:00) } ] ]}]你知道我该怎么做吗?
1 回答
www说
TA贡献1775条经验 获得超8个赞
受约束的预加载方法的关键是连接到事件表,以便我们可以比较 date_end 和 end 字段。假设您有多对多的人参加活动,您可以按如下方式执行此操作:
Event::with(['persons.health_items' => function($query){ $query->select('health_items.*') ->join('persons', 'persons.id', '=', 'health_items.person_id') ->join('event_person', 'event_person.person_id', '=', 'persons.id') ->join('events', 'events.id', '=', 'event_person.event_id') ->whereRaw('`health_items`.`date_end` = `events`.`end`'); }])->get();
- 1 回答
- 0 关注
- 106 浏览
添加回答
举报
0/150
提交
取消