1

I am using the spatie/laravel-query-builder to make a scope filter. In the documentation I read this:

You can even pass multiple parameters to the scope by passing a comma separated list to the filter:

GET /events?filter[starts_between]=2018-01-01,2018-12-31

So I made a scope filter in my model that looks like this:

public function scopeStartsBetween(Builder $query, $dates): Builder
{
    dd($dates);

    return $query->whereBetween('created_at', array($dates[0], $dates[1]));
}

My request looks like this:

http://127.0.0.1:8000/?filter[starts_between]=2018-10-15,2018-10-17

The result of the dd($dates) is this:

"2018-10-15"

So the comma seperated list for multiple parameters doesn't work, or am I doing something wrong?

Michael
  • 556
  • 1
  • 8
  • 27

2 Answers2

6

Class FiltersScope return ...$values

explaining what this mean by link Meaning of Three dot (...) in PHP

In Model scope function we must use same construction, for example

public function scopeStartsBetween(Builder $query, ...$dates): Builder
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Petro Kozlov
  • 61
  • 1
  • 2
3

The issue is that the multiple parameters should be handled as separate parameters for the scope function:

public function scopeStartsBetween(Builder $query, $from, $to): Builder
{
    return $query->whereBetween('created_at', array(Carbon::parse($from), Carbon::parse($to)));
}

This does the trick.

Michael
  • 556
  • 1
  • 8
  • 27