Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devionti's avatar

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);
            }),
        ];
    }
0 likes
0 replies

Please or to participate in this conversation.