danimohamadnejad's avatar

how to forbid user from model specific property?

hello. in my ecommerce application I want both user and admin to be able to update an Order model. user should update its order model all properties except to "stateId" and admin should only be able to update "stateId" of every order model. now I want both roles use the same PUT route for updating. what is best practice? thank you in advance

0 likes
5 replies
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@danimohamadnejad you can conditionally change validation rules and then pass validated data to update method:

$rules = [
   //...
];

if($request->user()->hasRole('admin')) { // your might be different
    $rules = ['stateid' => 'required'];
}

$data = $request->validate($rules);

// fetch model

$model->update($data);

danimohamadnejad's avatar

are you using above solution user cannot update stateId property? because I am using mass assignment and I feel user can change form and add stateId .

Please or to participate in this conversation.