wip
This commit is contained in:
123
app/Nova/Lenses/MostSoldProducts.php
Normal file
123
app/Nova/Lenses/MostSoldProducts.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Nova\Lenses;
|
||||
|
||||
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
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 MostSoldProducts extends Lens
|
||||
{
|
||||
/**
|
||||
* Get the displayable name of the lens.
|
||||
*/
|
||||
public function name(): string
|
||||
{
|
||||
return __('Most sold products');
|
||||
}
|
||||
|
||||
/**
|
||||
* The columns that should be searched.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $search = [];
|
||||
|
||||
/**
|
||||
* Get the query builder / paginator for the lens.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
*/
|
||||
public static function query(LensRequest $request, $query): mixed
|
||||
{
|
||||
$query->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('
|
||||
<div class="flex items-center">
|
||||
<span class="text-xs px-1 inline-flex leading-5 font-semibold rounded-full %s" style="padding-right: 7px;padding-left: 7px;">
|
||||
%s
|
||||
</span>
|
||||
</div>
|
||||
',
|
||||
$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';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user