Get filter by date, using the table date as the options

Table has a report_week, which is the same for several records, the day the report ran. I can get a list of distinct values for the options but I cannot see how to adjust the query with that data.
SelectFilter::make('report_week')
->options(LeadGeneration::where('report_week', '!=', null)
->distinct('report_week')->pluck('report_week')),
SelectFilter::make('report_week')
->options(LeadGeneration::where('report_week', '!=', null)
->distinct('report_week')->pluck('report_week')),
This gives me a set of report weeks to select. How do I use this in the $query?
Solution:
Good guess.... ```SelectFilter::make('report_week') ->options(LeadGeneration::where('report_week', '!=', null) ->orderBy('report_week', $direction) ->distinct('report_week') ->pluck('report_week', 'report_week') )...
Jump to solution
9 Replies
Dennis Koch
Dennis Koch6mo ago
I think you should be able to accept $data as a param: ->query($query, $data) =>
ddoddsr
ddoddsrOP6mo ago
Yes you are correct, I must be dealing with the timezone or something. the following code 'Selects' correctly but the query never effects the list.
SelectFilter::make('report_week')
->options(LeadGeneration::where('report_week', '!=', null)
->orderBy('report_week', $direction)
->distinct('report_week')
->pluck('report_week') )
->query(function (Builder $query, array $data): Builder {
if (Arr::exists($data, 'report_week') ) {
return $query->where('report_week',$data['report_week']);
} else {
return $query;
}
})
SelectFilter::make('report_week')
->options(LeadGeneration::where('report_week', '!=', null)
->orderBy('report_week', $direction)
->distinct('report_week')
->pluck('report_week') )
->query(function (Builder $query, array $data): Builder {
if (Arr::exists($data, 'report_week') ) {
return $query->where('report_week',$data['report_week']);
} else {
return $query;
}
})
The table view always returns all values in the table.
Dennis Koch
Dennis Koch6mo ago
Is the ->query() callback called at all?
ddoddsr
ddoddsrOP6mo ago
Yes, put in a logger the if is not firing ,
if (Arr::exists($data, 'report_week') ) {
if (Arr::exists($data, 'report_week') ) {
Dennis Koch
Dennis Koch6mo ago
So what's $data ? Shouldn't this be a single value?
ddoddsr
ddoddsrOP6mo ago
Actually the $data returns in $data['value]. a 0 or a 1 rather than the string
Dennis Koch
Dennis Koch6mo ago
I guess because you didn't provide a key for pluck :>pluck('report_week', 'report_week')
Solution
ddoddsr
ddoddsr6mo ago
Good guess....
SelectFilter::make('report_week')
->options(LeadGeneration::where('report_week', '!=', null)
->orderBy('report_week', $direction)
->distinct('report_week')
->pluck('report_week', 'report_week') )
->query(function (Builder $query, array $data): Builder {
if (Arr::exists($data, 'value') ) {
return $query->where('report_week',$data['value']);
} else {
return $query;
}
})
SelectFilter::make('report_week')
->options(LeadGeneration::where('report_week', '!=', null)
->orderBy('report_week', $direction)
->distinct('report_week')
->pluck('report_week', 'report_week') )
->query(function (Builder $query, array $data): Builder {
if (Arr::exists($data, 'value') ) {
return $query->where('report_week',$data['value']);
} else {
return $query;
}
})
ddoddsr
ddoddsrOP6mo ago
Works like it should!
Want results from more Discord servers?
Add your server