laravel / nova-issues

556 stars 34 forks source link

Method canSee() expects Request, NovaRequest given #4209

Closed sirikkoster closed 2 years ago

sirikkoster commented 2 years ago

Description:

Example code:


<?php namespace App\Nova;

use Laravel\Nova\Http\Requests\NovaRequest;

class NovaModel extends Resource
{
    /**
     * Get the filters available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     *
     * @return  array
     */
    public function filters(NovaRequest $request): array
    {
        return [
            (new CustomFilter)->canSee(function ($request)
            {
                return !$request->has('viaResource');
            })
        ];
    }
}
BerkanYildiz commented 2 years ago

Add: use Illuminate\Http\Request; Modify: canSee(function ($request) to canSee(function (Request $request)

crynobone commented 2 years ago

See https://github.com/laravel/nova-issues/issues/3903#issuecomment-1092694492

sirikkoster commented 2 years ago

Or fix the comment of the function "canSee" in "AuthorizedToSee.php"

Casting to Request also fails in phpstan

<?php

namespace Laravel\Nova;

use Closure;
use Illuminate\Http\Request;

trait AuthorizedToSee
{
    /**
     * The callback used to authorize viewing the filter or action.
     *
     * @var (\Closure(\Illuminate\Http\Request):bool)|null
     */
    public $seeCallback;

    /**
     * Determine if the filter or action should be available for the given request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    public function authorizedToSee(Request $request)
    {
        return $this->seeCallback ? call_user_func($this->seeCallback, $request) : true;
    }

    /**
     * Set the callback to be run to authorize viewing the filter or action.
     *
     * @param  \Closure(\Illuminate\Http\Request|Laravel\Nova\Http\Requests\NovaRequest:bool  $callback
     * @return $this
     */
    public function canSee(Closure $callback)
    {
        $this->seeCallback = $callback;

        return $this;
    }
}
crynobone commented 2 years ago

phpstan can't 100% be smart about this, changing the typehint would cause other issue in other place.

mishavantol commented 1 year ago

Not using phpstan, phpstorm doesn't understands TAuthoriseCallback:

image