Skip to content

Instantly share code, notes, and snippets.

@czemu

czemu/MostPopularRecipes.php Secret

Created Mar 6, 2020
Embed
What would you like to do?
<?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