I have a Settings nova Resource and a SettingsPolicy like below for this resource. The main goal of the policy is to hide the create button when there is already one Setting. The issue is that after adding the policy after update the resource I get a 403 error:
class SettingsPolicy
{
use HandlesAuthorization;
public function create()
{
return Settings::count() === 0;
}
public function update()
{
return true;
}
public function delete()
{
return true;
}
}
@sr57 I just resisted it on the AuthServiceProvider but it seems that is not working, dd($this->registerPolicies()) shows always null. And it still shows the 403 error. Thanks!
class SettingsPolicy
{
use HandlesAuthorization;
public function create()
{
return Settings::count() === 0;
}
public function update()
{
return true;
}
public function delete()
{
return true;
}
}
I tried to register the policy on the AuthServiceProvider but it seems that is not working, dd($this->registerPolicies()) shows always null. And it still shows the 403 error.
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
'App\Models\ Settings' => 'App\Policies\SettingsPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
dd($this->registerPolicies()); // shows always null
}
}
i am a newbie in Laravel. but trying. If wrong anyone can correct me.
As per i know stopping the script in boot does always returns "null". That ain't matter.
check if you're calling authorize() like this: $this->authorize('create',Settings::class)
Laravel always states that you need to provide one Model atleast in methods without models:
Ex:
PostPolicy.php
public function createPost(User $user)
{
return $user->hasRole(['writer','admin']);
}