我嘗試從數(shù)據(jù)庫(kù)中獲取事件$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();我想過(guò)濾我的關(guān)系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應(yīng)該是我的查詢(xún)的第一部分 ( Event::get)結(jié)果示例:[{ / 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說(shuō)
TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
受約束的預(yù)加載方法的關(guān)鍵是連接到事件表,以便我們可以比較 date_end 和 end 字段。假設(shè)您有多對(duì)多的人參加活動(dòng),您可以按如下方式執(zhí)行此操作:
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 關(guān)注
- 145 瀏覽
添加回答
舉報(bào)
0/150
提交
取消