-
-
Save czemu/fdd599a5803fd04951dda08801e0860c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Nova\Lenses; | |
use Laravel\Nova\Fields\ID; | |
use Illuminate\Http\Request; | |
use Laravel\Nova\Fields\Text; | |
use Laravel\Nova\Fields\Number; | |
use Laravel\Nova\Lenses\Lens; | |
use Laravel\Nova\Http\Requests\LensRequest; | |
class MostPopularRecipes extends Lens | |
{ | |
/** | |
* 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) | |
{ | |
$direction = $request->orderByDirection ?? 'DESC'; | |
$orderBy = $request->orderBy ?? 'views'; | |
return $request->withOrdering($request->withFilters( | |
$query | |
->withCount('comments') | |
->withCount('ratings') | |
->orderBy($orderBy, $direction) | |
)); | |
} | |
/** | |
* Get the fields available to the lens. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return array | |
*/ | |
public function fields(Request $request) | |
{ | |
return [ | |
ID::make('ID', 'id')->sortable(), | |
Text::make('Name', 'name')->sortable(), | |
Number::make('Views', 'views')->sortable(), | |
Number::make('Ratings', 'ratings_count'), | |
Number::make('Comments', 'comments_count') | |
]; | |
} | |
/** | |
* Get the URI key for the lens. | |
* | |
* @return string | |
*/ | |
public function uriKey() | |
{ | |
return 'most-popular-recipes'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment