nova-prepopulate-searchable icon indicating copy to clipboard operation
nova-prepopulate-searchable copied to clipboard

prepopulate limit

Open bsormagec opened this issue 6 years ago • 3 comments

is there any way to add limit this query ? i wanna limit prepopulate to 10 record. Thanks.

bsormagec avatar Jun 18 '19 18:06 bsormagec

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);
	}
}

freshway avatar Jul 16 '19 12:07 freshway

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);
    }

codebykyle avatar Jul 18 '19 06:07 codebykyle

I'll look into adding functionality for this soon. Thanks for the suggestion!

mikebronner avatar Sep 02 '20 18:09 mikebronner