-
-
Save Namrata199/63f6b30b7b73bef395551033e4edd75e to your computer and use it in GitHub Desktop.
Date filter in api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\Pages; | |
use App\Http\Controllers\App\Dashboard\v1\ReportDashboardController; | |
use App\Models\Flavor; | |
use Filament\Forms\Components\DatePicker; | |
use Filament\Pages\Page; | |
use Filament\Tables\Actions\BulkAction; | |
use Filament\Tables\Columns\TextColumn; | |
use Filament\Tables\Concerns\HasFilters; | |
use Filament\Tables\Concerns\InteractsWithTable; | |
use Filament\Tables\Contracts\HasTable; | |
use Filament\Tables\Filters\Filter; | |
use Filament\Tables\Filters\Layout; | |
use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Http\Request; | |
use pxlrbt\FilamentExcel\Actions\Tables\ExportBulkAction; | |
use pxlrbt\FilamentExcel\Exports\ExcelExport; | |
use pxlrbt\FilamentExcel\Columns\Column; | |
use Illuminate\Support\Str; | |
class FlavorReport extends Page implements HasTable | |
{ | |
use InteractsWithTable, HasFilters; | |
protected static ?string $navigationIcon = 'heroicon-o-document-text'; | |
protected static string $view = 'filament.pages.flavor-report'; | |
public $startDate, $endDate; | |
public $apiData = null; | |
protected function getTableQuery(): Builder | |
{ | |
return Flavor::where('active', true); | |
} | |
public function updated($name): void | |
{ | |
if (Str::of($name)->contains('tableFilters.filters')) { | |
$startDate = data_get($this->tableFilters, 'filters.start_at'); | |
$endDate = data_get($this->tableFilters, 'filters.end_at'); | |
$report = (new ReportDashboardController)->cakeFlavorProduction(new Request([ | |
$startDate, | |
$endDate, | |
])); | |
$this->apiData = json_decode($report->getContent(), true)['data']; | |
} | |
} | |
protected function getTableColumns(): array | |
{ | |
return [ | |
TextColumn::make('name'), | |
TextColumn::make('pound') | |
->getStateUsing(function ($record) { | |
return $this->apiData ? collect($this->apiData)->firstWhere('id', $record->id)['pound'] ?? 0 : 0; | |
}), | |
]; | |
} | |
protected function getTableFilters(): array | |
{ | |
return [ | |
Filter::make('filters') | |
->form([ | |
DatePicker::make('start_at')->default(now()), | |
DatePicker::make('end_at')->default(now()), | |
])->columns(2) | |
->columnSpan(2) | |
]; | |
} | |
protected function getTableBulkActions(): array | |
{ | |
return [ | |
ExportBulkAction::make() | |
]; | |
} | |
// protected function getTableFilters(): array | |
// { | |
// return [ | |
// Filter::make('created_at') | |
// ->form([ | |
// DatePicker::make('from'), | |
// DatePicker::make('to'), | |
// ])->columns(2) | |
// ->columnSpan(2) | |
// ->query(function (Builder $query, array $data): Builder { | |
// return $query | |
// ->when( | |
// $data['from'], | |
// fn (Builder $query, $date): Builder => $query->whereDate('created_at', '>=', $date), | |
// ) | |
// ->when( | |
// $data['to'], | |
// fn (Builder $query, $date): Builder => $query->whereDate('created_at', '<=', $date), | |
// ); | |
// }) | |
// ]; | |
// } | |
protected function getTableFiltersLayout(): ?string | |
{ | |
return Layout::AboveContent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment