mercredi 22 août 2018

Laravel policies: Lookup by UUID

My Laravel Models contain both IDs and UUIDs. The IDs are not exposed to the end-user. My policies are working as expected, except for the UPDATE policy, whose $model always is null. The policy is defined as follows:

class MyPolicy
{
    use HandlesAuthorization;
    public function update($user, $model)
    {
        // Model is always null
        return $user->uuid == $model->uuid;
    }
}

It seems that Laravel is trying to lookup the model by ID as opposed to UUID.

The AuthServiceProvider entry for the policy is as follows:

Gate::resource('users', 'App\Policies\MyPolicy');

And inside my UserModel, I have:

public function getRouteKeyName()
{
   return 'uuid';
}

Is there any way that I can force Laravel to lookup by UUID for policies without changing the primaryKey field in the model?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire