Laravel Nova - How to determine the view (index, detail, form) you are in for a resource's computed field?

后端 未结 5 701
野的像风
野的像风 2021-01-19 07:21

I would like to return a different result for a computed field when viewing the index view than when viewing the detail view of a resource.

Basically something like

5条回答
  •  星月不相逢
    2021-01-19 08:03

    I added this little helper class

    namespace App\Helpers;
    
    class CurrentResourceAction {
        public static function isIndex($request)
        {
            return $request instanceof \Laravel\Nova\Http\Requests\ResourceIndexRequest;
        }
        public static function isDetail($request)
        {
            return $request instanceof \Laravel\Nova\Http\Requests\ResourceDetailRequest;
        }
        public static function isCreate($request)
        {
            return $request instanceof \Laravel\Nova\Http\Requests\NovaRequest &&
                $request->editMode === 'create';
        }
        public static function isUpdate($request)
        {
            return $request instanceof \Laravel\Nova\Http\Requests\NovaRequest &&
                $request->editMode === 'update';
        }
    }
    

    you can call it anywhere you need to

提交回复
热议问题