Mar 7, 2022
0
Level 2
Lens Generate summary using dynamic columns
I am trying to make a summary report. I am using Lens to generate the summary. The problem is the number of columns and labels are not predetermined.
Basically each record has remarks column and a location column.
What I want is the generated lens to have new columns based on the number of records if they are not null. For example
|Remarks |London | Manchester|...|Total|
|--- | --- | ---|
|Valid|17|20|...|37|
|Valid for 6 Months|5|12|...|17|
|Expires in 2 months|9|3|...|12|
There are tons of locations so I cannot create statically 40 columns specially if some are null.
/**
* Get the query builder / paginator for the lens.
*
* @param \Laravel\Nova\Http\Requests\LensRequest $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return mixed
*/
public static function query(LensRequest $request, $query)
{
return $request->withOrdering($request->withFilters(
$query->select(self::columns())
->join('users', 'certifications.user_id', '=', 'users.id')
->orderBy('location', 'desc')
->groupBy('certifications.remarks')
));
}
/**
* Get the columns that should be selected.
*
* @return array
*/
protected static function columns()
{
return [
'certifications.remarks',
'certifications.location',
DB::raw('count(certifications.location) as total'),
];
}
/**
* Get the fields available to the lens.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
Text::make('Remarks', 'remarks'),
//Text::make('Location', 'location'),
Number::make('count', 'total', function ($value) {
return number_format($value, 2);
}),
];
}
Please or to participate in this conversation.