阅读手册 https://nova.laravel.com/docs/lenses/defining-lenses.html 在 Laravel 10 / nova 4.27 应用程序中,我使用命令创建一个新镜头
php artisan nova:lens Orders/MostActiveUsersWithProcessingOrders
我填充创建的类 app/Nova/Lenses/Orders/MostActiveUsersWithProcessingOrders.php 的方法 (带有登录代码):
<?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;
}
}
结果我在仪表板页面中没有任何数据(包括名称方法中的文本)。
检查日志我只看到几行:
[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 ::
所以方法
query
、fields
、columns
甚至都不会被调用。为什么这样?
附加信息:
我将我的 MostActiveUsersWithProcessingOrders 镜头组件添加到 app/Nova/Dashboards/Main.php 文件为:
public function cards()
{
return [
// new OrdersInInvoiceWithExpireDate,
//
// new OrdersByStatusPieChart, new OrdersCompleted, new OrdersCompletedByManagerByDays,
// new NewReleases,
//
new MostActiveUsersWithProcessingOrders,
// new Help,
];
}
使用此代码,我的
Dashboard
为空。如果我取消注释其余组件,我会在 Dashboard
页面上看到它们。
MostActiveUsersWithProcessingOrders 是唯一的 Lens 组件,只有它才会出现此类问题。
看起来要在仪表板页面上添加自定义数据,我需要使用以下命令创建新卡:
php artisan nova:card vendor/package
但不是镜头,正如我尝试的那样