A
A
Anatoly2018-02-08 20:26:16
Laravel
Anatoly, 2018-02-08 20:26:16

Why is laravel policy not working?

Good afternoon!
Prompt, created RolesPolicy.
added the following

protected $policies = [
      Roles::class => RolesPolicy::class,
   ];

and when called in a controller method
$role = Menu::where('route', $this->currentRouteName())->first();
        $roles = new \App\Models\Roles;
        $this->authorize('view', $request->user(), $roles);

The RolesPolicy.php itself is here:
namespace App\Policies;

use App\Models\User;
use App\App\Models\Roles;
use Illuminate\Auth\Access\HandlesAuthorization;

class RolesPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view the roles.
     *
     * @param  \App\Models\User  $user
     * @param  \App\App\Models\Roles  $roles
     * @return mixed
     */
    public function view(User $user, Roles $roles)
    {
        return false;
    }
}

Gives an error prntscr.com/ibzar6
I did everything according to off documentation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Timoshenkov, 2018-06-08
@t1gor

The user is injected into the Policy automatically, you only need to specify what you want to do and with what:
$this->authorize('view', $roles);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question