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('