0

Reading manuals https://nova.laravel.com/docs/lenses/defining-lenses.html in Laravel 10 / nova 4.27 app I create a new lens with command

php artisan nova:lens Orders/MostActiveUsersWithProcessingOrders

and I fill methods of the created class app/Nova/Lenses/Orders/MostActiveUsersWithProcessingOrders.php (with loging code):

<?php

namespace App\Nova\Lenses\Orders;

use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\LensRequest;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Lenses\Lens;

class MostActiveUsersWithProcessingOrders extends Lens
{
    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [];

    /**
     * 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)
    {
       \Log::info(' -1 query ::');

        return $request->withOrdering($request->withFilters(
            $query->select(self::columns())
                ->join('orders', 'users.id', '=', 'orders.user_id')
                ->groupBy('users.id', 'users.name')
                ->withCasts([
                    'orders_count' => 'float',
                ])
        ), fn ($query) => $query->orderBy('orders_count', 'desc'));
    }

    /**
     * Get the columns that should be selected.
     *
     * @return array
     */
    protected static function columns()
    {
        \Log::info(' -1 columns ::');
        return [
            'users.id',
            'users.name',
            DB::raw('count(orders.id) as orders_count'),
        ];
    }

    /**
     * Get the fields available to the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        \Log::info(' -1 fields ::');
        return [
            ID::make('ID', 'id'),
            Text::make('Name', 'name'),

            Number::make('orders_count', 'orders_count', function ($value) {
                return $value;
            }),
        ];
    }

    /**
     * Get the cards available on the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        \Log::info(' -1 cards ::');
        return [];
    }

    /**
     * Get the filters available for the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        \Log::info(' -1 filters ::');
        return [];
    }

    /**
     * Get the actions available on the lens.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        \Log::info(' -1 actions ::');
        return parent::actions($request);
    }

/* Title of the card
*
* @return string
*/
    public function name(): string
    {
        \Log::info(' -1 name ::');
        return 'Active users with biggest number of processing orders';
    }

    /**
     * Get the URI key for the lens.
     *
     * @return string
     */
    public function uriKey()
    {
        \Log::info(' -1 uriKey ::');
        return 'orders-most-active-users-with-processing-orders';
    }

    /**
     * Determine if the given resource is authorizable.
     *
     * @return bool
     */
    public static function authorize()
    {
        \Log::info(' -1 authorize ::');
        return true;
    }

}

As result I have no any data in Dashboard page(including text in name method).

Checking log I see only lines :

[2024-11-17 08:09:22] local.INFO:  -1 authorize ::
[2024-11-17 08:09:22] local.INFO:  -1 name ::
[2024-11-17 08:09:22] local.INFO:  -1 uriKey ::

So methods query, fields, columns are not even called. Why So ?

Additive info :

I added my MostActiveUsersWithProcessingOrders lens component into app/Nova/Dashboards/Main.php file as :

    public function cards()
    {
        return [
//            new OrdersInInvoiceWithExpireDate,
//
//            new OrdersByStatusPieChart, new OrdersCompleted, new OrdersCompletedByManagerByDays,
//            new NewReleases,
//

            new MostActiveUsersWithProcessingOrders,

//            new Help,
        ];
    }

With this code my Dashboard is empty. If I uncomment the rest components I see them on Dashboard page.

MostActiveUsersWithProcessingOrders is the only Lens component and such problems only with it.

5
  • 1
    Just a suggestion: I think you can rewrite your query() method like ` return $request->withFilters( $query->select(self::columns()) ->join('orders', 'users.id', '=', 'orders.user_id') ->groupBy('users.id', 'users.name') ->withCasts(['orders_count' => 'float']) ->orderBy('orders_count', 'desc') );` Commented Nov 17, 2024 at 9:21
  • @Yousha Aleayoub , I tried to remake in your way : anyway query() method is not called and no any data in dashboard Commented Nov 17, 2024 at 12:29
  • 1
    @mstdmstd did you add that lens to the main nova resource file? As in are you able to select viewing the resource through that lens from the web page Commented Nov 19, 2024 at 12:44
  • I try to put MostActiveUsersWithProcessingOrders lens under Dasboard page. Can I do it anyway ? Do you mean that I can use it only in resource page ( url nova/resources/orders ). Please, clarify that. Commented Nov 20, 2024 at 5:40
  • Please read Additive info Commented Nov 25, 2024 at 7:11

1 Answer 1

0

Looks like tp add custom data on dashboard page I need to create a new card with command like :

php artisan nova:card vendor/package

But not lense, as I tried

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.