Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kokarat's avatar

Can I Custom query on resources?

I need to use resources feature without custom tools but I need to use a custom query it possible to do that?

0 likes
3 replies
aurawindsurfing's avatar
Level 50

Hi,

I guess this is what you are looking for:

https://nova.laravel.com/docs/1.0/resources/authorization.html#index-filtering

/**
 * 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)
{
    return $query->where('user_id', $request->user()->id);
}
yaroslawww's avatar

Also you cam override indexQuery or any other queries (detailQuery .... etc)

    public static function indexQuery(NovaRequest $request, $query)
    {
         $query = parent::indexQuery($request, $query);

         return $query->withCount('subscriptions');
    }
1 like

Please or to participate in this conversation.