亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據此查詢的結果過濾雄辯的查詢

根據此查詢的結果過濾雄辯的查詢

PHP
Cats萌萌 2023-07-08 20:20:38
我嘗試從數據庫中獲取事件$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();


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號