I have a problem updating the information of a role. In my database I have the attribute nombre_rol
as unique
. When registering a new role it works perfectly but when updating the same record it throws me the error that the Role element is already in use.
Model Role
class Rol extends Model
{
protected $table = 'rol';
protected $primaryKey = 'idrol';
public $timestamps =false;
protected $fillable =['nombre_rol, descripcion, estado, fecha'];
protected $guarded =[];
}
RoleRequest
public function rules()
{
return [
'nombre_rol'=>'required|unique:Rol,nombre_rol',
'descripcion'=>'required'
];
}
I am using laravel 5.2 and according to the documentation it says that to ignore I must put the following
'email' => 'nombre_rol'=>'required|unique:Rol,nombre_rol,'.$user->id.',user_id',
But in my case I get an undefined variable error, I don't even understand where I get the $user->id variable from.
You are using the wrong values in the validation syntax:
In your case it should be something like this, assuming you're not using Model Binding:
If your route is something like
Route::put('actualizar-rol/{id}', 'Controlador@metodo');
So the validation would be
If you use Route Model Binding, it would be something like this:
Route:
Route::put('actualizar-rol/{rol}', 'Controlador@metodo');
and the validation:
It worked 100% for me using the validation inside my update method, since in my store method I don't send the id.
I leave the code in case it helps someone. Greetings.