Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Meaulnes's avatar

How to filter collection based on date comparison ?

I have a period table that has: a start column of type date an end column of type date a current column of type boolean (only one period has this current column set to 1)

Then I have an events table with a date column of type date

In a controller I want to get a collection of events whose date column is betwen the start and the end of the current period i.e. the period with 1 in column current.

I do this

$period=Period::where('current',1)->get();
$start=$period[0]->start;
$end=$period[0]->end;
$events=EventResource::collection(Event::whereDate('date','>=',$start)->whereDate('date','<=',$end))->get();

The last line make the controller crash with this message.

"message": "Call to undefined method Illuminate\Database\Eloquent\Builder::mapInto()", "exception": "BadMethodCallException",

Whatis wrong here?

0 likes
4 replies
jaseofspades88's avatar

You have a column named the exact same thing as a magic method would be able to check. In Laravel if you use where{Field}() then it will try to resolve the 'where field'. e.g. ->whereName('John') will resolve to ->where('name', 'John') In your example I would consider using ->whereBetween('date', [$start, $end]) if changing the database is out of the question..

Meaulnes's avatar

@jaseofspades88 Thank you for your answer . I tried your suggestion with whereBetween

$events=EventResource::collection(Event::whereBetween('date',[$start,$end]))->get();

also

$events=EventResource::collection(Event::where('date','>=',$start)->where('date','<=',$end))->get();

but neither works.

Should I understand that I have to change the name of the columns (date) into anything else?

Meaulnes's avatar
Meaulnes
OP
Best Answer
Level 1
$events=EventResource::collection(Event::where('date','>=',$start)->where('date','<=',$end)->get());

was the good expression. Note the closing parenthesis at the end.

amitsolanki24_'s avatar

Try this $events = $period=Period::where('current',1)->select('starr','end')->first(); $start=$period->start; $end=$period->end; $events=Event::whereDate('date','>=',$start)->whereDate('date','<=',$end)->get(); $events=EventResource::collection($events);

Please or to participate in this conversation.