with('media'); return $request->withOrdering($request->withFilters( $query ->select('products.*', DB::raw('SUM(order_items.quantity) AS sold_count')) ->join('order_items', 'order_items.product_id', '=', 'products.id') ->join('orders', function ($join) { $join->on('orders.id', '=', 'order_items.order_id') ->on('orders.status', '=', DB::raw("'completed'")); }) ->groupBy('products.id') ->orderBy('sold_count', 'DESC') ->withCasts([ 'sold_count' => 'int', ]) )); } /** * Get the fields available to the lens. * * @return array */ public function fields(NovaRequest $request) { return [ ID::make(__('ID'), 'id')->sortable(), Images::make(__('Image'), 'uploads')->conversionOnIndexView('thumb200x200'), Text::make(__('Name'), 'name')->sortable(), Number::make(__('Price'), 'price_amount')->sortable(), Text::make(__('Count'), 'sold_count', function ($value) { return sprintf('
%s
', $value < 10 ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-darker', $value ); })->asHtml(), ]; } /** * Get the cards available on the lens. * * @return array */ public function cards(NovaRequest $request) { return []; } /** * Get the filters available for the lens. * * @return array */ public function filters(NovaRequest $request) { return []; } /** * Get the actions available on the lens. * * @return array */ public function actions(NovaRequest $request) { return parent::actions($request); } /** * Get the URI key for the lens. * * @return string */ public function uriKey() { return 'most-sold-products'; } }