I need to collect my ticket sections from the array event, and my condition is
if 'custom_times' == 'yes'
then pick up ticket section with only
section_end_time < current date
Sample array Format
{"event_id":1,"ticket_type":"event", "custom_times":"yes",.....,"ticket_section":[{"section_id":1,"section_end_time":"2019-10-10 12:12:12",..... }]}
Query
$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);
});
But the above query not return the actual result.
Thanks
I think you've confused the collection with the query builder.
The when method of the collection needs a boolean value to work and can't work with a condition on the collection members.
Here's how you can do what you need:
$ticket_sections = collect($event->ticket_sections)
->where('ticket_type', 'event')
->where('hide_online', 'no')
->filter(function($section) use ($event) {
return $section->custom_times !== 'yes'
|| $section->section_end_time < $event->current_date_time;
});