diff --git a/src/Components/Filters/Builders/DatePicker.php b/src/Components/Filters/Builders/DatePicker.php index 33c690b6..fb478d7c 100644 --- a/src/Components/Filters/Builders/DatePicker.php +++ b/src/Components/Filters/Builders/DatePicker.php @@ -16,8 +16,8 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in /** @var array $values */ [$startDate, $endDate] = [ - 0 => Carbon::parse($values[0])->format('Y-m-d'), - 1 => Carbon::parse($values[1])->format('Y-m-d'), + 0 => Carbon::parse($values['start'])->format('Y-m-d'), + 1 => Carbon::parse($values['end'])->format('Y-m-d'), ]; if (data_get($this->filterBase, 'builder')) { diff --git a/src/Components/Filters/Builders/DateTimePicker.php b/src/Components/Filters/Builders/DateTimePicker.php index 2f33c7a2..4bb35bb4 100644 --- a/src/Components/Filters/Builders/DateTimePicker.php +++ b/src/Components/Filters/Builders/DateTimePicker.php @@ -16,8 +16,8 @@ public function builder(EloquentBuilder|QueryBuilder $builder, string $field, in /** @var array $values */ [$startDate, $endDate] = [ - 0 => Carbon::parse($values[0]), - 1 => Carbon::parse($values[1]), + 0 => Carbon::parse($values['start']), + 1 => Carbon::parse($values['end']), ]; if (data_get($this->filterBase, 'builder')) { @@ -36,8 +36,8 @@ public function collection(Collection $collection, string $field, int|array|stri { /** @var array $values */ [$startDate, $endDate] = [ - 0 => Carbon::parse($values[0]), - 1 => Carbon::parse($values[1]), + 0 => Carbon::parse($values['start']), + 1 => Carbon::parse($values['end']), ]; if (data_get($this->filterBase, 'collection')) { diff --git a/src/DataSource/Builder.php b/src/DataSource/Builder.php index 38c2f018..25aaa984 100644 --- a/src/DataSource/Builder.php +++ b/src/DataSource/Builder.php @@ -46,26 +46,54 @@ public function filter(): EloquentBuilder|QueryBuilder foreach ($this->powerGridComponent->filters as $filterType => $columns) { $columns = Arr::dot($columns); - foreach ($columns as $field => $value) { - if ($filterType == 'number') { - if (str($field)->contains('.start')) { - $value = [ - 'start' => $value, - ]; + // convert array:2 [ + // "dishes.produced_at.start" => "2021-03-03", + // "dishes.produced_at.end" => "2021-03-01" + //] to + // array:2 [ + // "dishes.produced_at" => ["start" => "2021-03-03"], + // "dishes.produced_at.end" => ["start" => "2021-03-01"] + //] and + // convert array:2 [ + // "dishes.produced_at.0" => "2021-03-03", + // "dishes.produced_at.1" => "2021-03-01" + //] to + // array:2 [ + // "dishes.produced_at" => [0 => "2021-03-03"], + // "dishes.produced_at" => [1 => "2021-03-01"] + //] + + $newColumns = []; + + foreach ($columns as $key => $value) { + $parts = explode('.', $key); + $lastPart = end($parts); + + if (is_numeric($lastPart) && intval($lastPart) == $lastPart) { + array_pop($parts); + $prefix = implode('.', $parts); + + if (!isset($newColumns[$prefix])) { + $newColumns[$prefix] = []; } - if (str($field)->contains('.end')) { - $value = [ - 'end' => $value, - ]; + $index = intval($lastPart); + + $newColumns[$prefix][$index] = $value; + } elseif ($lastPart === 'start' || $lastPart === 'end') { + $prefix = implode('.', array_slice($parts, 0, -1)); + + if (!isset($newColumns[$prefix])) { + $newColumns[$prefix] = []; } - $field = str($field)->replace('.start', '') - ->replace('.end', '') - ->trim() - ->toString(); + $newColumns[$prefix][$lastPart] = $value; + } else { + $newColumns[$key] = $value; } + } + foreach ($newColumns as $field => $value) { $this->query->where(function ($query) use ($filterType, $field, $value, $filters) { $filter = function ($query, $filters, $filterType, $field, $value) { $filter = $filters->filter(function ($filter) use ($field) { diff --git a/src/Traits/HasFilter.php b/src/Traits/HasFilter.php index 02f7d042..50bafc6d 100644 --- a/src/Traits/HasFilter.php +++ b/src/Traits/HasFilter.php @@ -230,12 +230,11 @@ public function datePickerChanged( $endDate->endOfDay()->setTimeZone($appTimeZone); } - $selectedDatesFormatted = [$startDate, $endDate]; - $this->enabledFilters[$field]['data-field'] = $field; $this->enabledFilters[$field]['label'] = $label; - $this->filters[$type][$field] = $selectedDatesFormatted; + $this->filters[$type][$field]['start'] = $startDate; + $this->filters[$type][$field]['end'] = $endDate; $this->filters[$type][$field]['formatted'] = $dateStr; diff --git a/tests/Feature/Filters/FilterDateTimeTest.php b/tests/Feature/Filters/FilterDateTimeTest.php index e9131f5f..acec076e 100644 --- a/tests/Feature/Filters/FilterDateTimeTest.php +++ b/tests/Feature/Filters/FilterDateTimeTest.php @@ -58,7 +58,7 @@ ->set('testFilters', [ Filter::datepicker('produced_at_formatted', 'produced_at'), ]) - ->set('filters', filterDate('produced_at', ['2021-03-03', '2021-03-01'])) + ->set('filters', filterDate('dishes.produced_at', ['2021-03-03', '2021-03-01'])) ->assertSee('No records found'); })->group('filters', 'filterDatePicker') ->with('filter_datetime_themes_with_join', 'filter_datetime_query_builder'); @@ -179,7 +179,10 @@ function filterDate(string $dataField, array $value): array { return [ 'date' => [ - $dataField => $value, + $dataField => [ + 'start' => $value[0], + 'end' => $value[1], + ], ], ]; } @@ -188,7 +191,10 @@ function filterDateTime(string $dataField, array $value): array { return [ 'datetime' => [ - $dataField => $value, + $dataField => [ + 'start' => $value[0], + 'end' => $value[1], + ], ], ]; }