Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

prepopulate limit #9

Open
bsormagec opened this issue Jun 18, 2019 · 3 comments
Open

prepopulate limit #9

bsormagec opened this issue Jun 18, 2019 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@bsormagec
Copy link

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

@freshway
Copy link

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

@codebykyle
Copy link

codebykyle commented Jul 18, 2019

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

@mikebronner mikebronner self-assigned this Sep 1, 2020
@mikebronner mikebronner added question Further information is requested enhancement New feature or request and removed question Further information is requested labels Sep 1, 2020
@mikebronner
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants