如何在Laravel排序中应用IF-Where条件
我需要从阵列事件中收集我的票证部分,我的条件是如何在Laravel排序中应用IF-Where条件,laravel,collect,laravel-6,Laravel,Collect,Laravel 6,我需要从阵列事件中收集我的票证部分,我的条件是 if 'custom_times' == 'yes' 然后,只需使用 section\u end\u timeticket\u sections)->映射(函数($sections){ 返回$sections; })->其中(‘票证类型’、‘事件’) ->其中('在线隐藏','否') ->当('custom_times'='yes',函数($query){ return$query->where('section\u end\u time',”
if 'custom_times' == 'yes'
然后,只需使用
section\u end\u time
样本数组格式
{"event_id":1,"ticket_type":"event", "custom_times":"yes",.....,"ticket_section":[{"section_id":1,"section_end_time":"2019-10-10 12:12:12",..... }]}
查询
$ticket_sections = collect($event->ticket_sections)->map(function($sections) {
return $sections;
})->where('ticket_type', 'event')
->where('hide_online', 'no')
->when('custom_times' == 'yes', function($query){
return $query->where('section_end_time', '<' ,$event->current_date_time);
});
$ticket\u sections=收集($event->ticket\u sections)->映射(函数($sections){
返回$sections;
})->其中(‘票证类型’、‘事件’)
->其中('在线隐藏','否')
->当('custom_times'='yes',函数($query){
return$query->where('section\u end\u time',”我想你把集合和查询生成器搞混了
集合的方法需要一个布尔值才能工作,并且不能使用集合成员上的条件
以下是如何满足您的需求:
$ticket\u sections=collect($event->ticket\u sections)
->其中(‘票证类型’、‘事件’)
->其中('在线隐藏','否')
->过滤器(函数($section)使用($event){
返回$section->custom_次!==“是”
||$section->section\u end\u time<$event->current\u date\u time;
});
谢谢你的回答,我是拉雷维尔的新手,这就是为什么会发生这种情况…,我将尝试你的答案。