prepopulate limit
is there any way to add limit this query ? i wanna limit prepopulate to 10 record. Thanks.
Based on the relatableQuery override function, I process requested filter.
When it is prepopulated there is no filter.
public static function relatableQuery(NovaRequest $request, $query)
{
if( ! $request->filled('search')){
return $query->limit(100);
}
}
Same use case. Looking into this, this field has a callback that passes a default "search" parameter into the field and fires the search function when the field is loaded. This, for the most part, just keeps the default Nova implementation of the BelongsTo field (it does however, overwrite the field, if the field is for whatever reason updated), and just fires its search.
Nova, by itself, doesn't offer pagination on the associatable endpoint. It calls the relatableQuery, as @freshway has pointed out.
The reason that works is because the search parameter wont be null when searching from Laravel Nova. It only performs the search once the user types something in. If its blank, we can assume its coming from the prepopulate call.
Because of this, you can actually apply that to all resources by overriding it in your default Resource class.
Put this in Resource.php:
/**
* Build a "relatable" query for the given resource.
*
* This query determines which instances of the model may be attached to other resources.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function relatableQuery(NovaRequest $request, $query)
{
if( !$request->filled('search')){
return parent::relatableQuery($request, $query)->limit(25);
}
return parent::relatableQuery($request, $query);
}
I'll look into adding functionality for this soon. Thanks for the suggestion!