最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

laravel nova - Why created lense is not shown in Dashboard page? - Stack Overflow

matteradmin7PV0评论

Reading manuals .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.

Reading manuals https://nova.laravel/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.

Share Improve this question edited Nov 25, 2024 at 7:11 mstdmstd asked Nov 17, 2024 at 6:23 mstdmstdmstdmstd 3,23322 gold badges87 silver badges192 bronze badges 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') );` – Yousha Aleayoub 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 – mstdmstd 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 – Jon Menard 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. – mstdmstd Commented Nov 20, 2024 at 5:40
  • Please read Additive info – mstdmstd Commented Nov 25, 2024 at 7:11
Add a comment  | 

1 Answer 1

Reset to default 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

Post a comment

comment list (0)

  1. No comments so far