Skip to content

Instantly share code, notes, and snippets.

@czemu
Created March 6, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czemu/fdd599a5803fd04951dda08801e0860c to your computer and use it in GitHub Desktop.
Save czemu/fdd599a5803fd04951dda08801e0860c to your computer and use it in GitHub Desktop.
<?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