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

后端 未结 5 696
野的像风
野的像风 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

    0 讨论(0)
  • 2021-01-19 08:04

    You can create two separate fields for index & details page.

    // ----- For Index page
    Text::make('Preview', function () {
        return \small_preview($this->image);
    })
    ->onlyOnIndex()
    ->asHtml(),
    
    // ----- For Detail page
    Text::make('Preview', function () {
        return \large_preview($this->image);
    })
    ->onlyOnDetail()
    ->asHtml(),
    
    0 讨论(0)
  • 2021-01-19 08:05

    You can check the class of the request:

    Text::make('Preview', function () use ($request) {
        if ($request instanceof \Laravel\Nova\Http\Requests\ResourceDetailRequest) {
            return \large_preview($this->image);
        }
    
        return \small_preview($this->image);
    });
    

    Otherwise, you can create your own viewIs function:

    // app/Nova/Resource.php
    
    /**
     * Check the current view.
     *
     * @param  string  $view
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @retrun bool
     */
    public function viewIs($view, $request)
    {
        $class = '\Laravel\Nova\Http\Requests\\Resource'.ucfirst($view).'Request';
    
        return $request instanceof $class;
    }
    

    Then you can do it like this:

    Text::make('Preview', function () use ($request) {
        if ($this->viewIs('detail', $request) {
            return \large_preview($this->image);
        }
    
        return \small_preview($this->image);
    });
    
    0 讨论(0)
  • 2021-01-19 08:06

    Unfortunately, @Chin's answer did not work for me as for Edit view the request class is just a basic Laravel\Nova\Http\Request class.

    My workaround to check if this is an index view is as follows:

    /**
     * Check if the current view is an index view.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest $request
     * @return bool
     */
    public function isIndex($request)
    {
        return $request->resourceId === null;
    }
    
    0 讨论(0)
  • 2021-01-19 08:15

    The NovaRequest class will soon be able to help, as the isResourceIndexRequest and isResourceDetailRequest are already in master.

    As the Nova repo is private I will keep you posted, when it will be usable.

    In the meantime I am falling back to helper methods on the Nova Resource class (app/Nova/Resource.php):

    namespace App\Nova;
    
    use Laravel\Nova\Http\Requests\ResourceDetailRequest;
    use Laravel\Nova\Http\Requests\ResourceIndexRequest;
    use Laravel\Nova\Resource as NovaResource;
    use Laravel\Nova\Http\Requests\NovaRequest;
    
    abstract class Resource extends NovaResource
    {
        // [...]
    
        /**
         * Determine if this request is a resource index request.
         *
         * @return bool
         */
        public function isResourceIndexRequest($request)
        {
            return $request instanceof ResourceIndexRequest;
        }
    
        /**
         * Determine if this request is a resource detail request.
         *
         * @return bool
         */
        public function isResourceDetailRequest($request)
        {
            return $request instanceof ResourceDetailRequest;
        }
    }
    

    Usage:

    public function fields(Request $request)
    {
        $fields = [
            // [...]
        ];
    
        if ($this->isResourceDetailRequest($request)) {
            if ($this->isResourceDetailRequest($request)) {
                $fields = array_merge($fields, [
                    // [...]
                ]);
            }
        }
    
        return $fields;
    }
    
    0 讨论(0)
提交回复
热议问题