I am trying to edit my users according to the type of user it will be administrator or editor, from the administration panel the admin user can change the role and assign editor or administrator to other users.
On my edit page I have an input right now where the user can put 0 or 1 depending on the role they want to assign.
- 1 --> administrator
- 0 --> editor (this is assigned by default to newly created users)
This is the current input:
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Tipo de usuario:</strong>
<input type="text" name="tipo_usuario" class="form-control" value="{{ $user->tipo_usuario }}">
</div>
</div>
By default when entering there is always a value already assigned and that we edit the user, what I intend is the following:
Create a select option where both options are shown, editor and administrator and the option saved for this user is selected by default, so that every time you want to edit some data and do not want to touch the role, it is not necessary and is already assigned by the value received.
I have tried to do the following, since in the database it must receive the number 1 or 0, I only want the option to be read visually so that the user knows the role they choose:
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Tipo de usuario:</strong>
<select class="form-control" name="tipo_usuario">
<option value="administrador"{{ $user->tipo_usuario == 1 ? 'selected' : ''}}>Administrador</option>
<option value="editor"{{ $user->tipo_usuario == 0 ? 'selected' : '' }}>Editor</option>
</select>
</div>
</div>
But it gives me an error and it does not save correctly.
ParseError syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ')' (View:viewpath/edit.blade.php)
I attach my controller:
public function edit(User $user)
{
return view('users.edit',compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\User $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required',
'surname' => 'required',
'email' => 'required',
'password' => 'required',
]);
$users = User::find($id);
$users->name = $request->get('name');
$users->surname = $request->get('surname');
$users->email = $request->get('email');
$users->tipo_usuario = $request->get('tipo_usuario');
$users->password = Hash::make($request->get('password'));
$users->update();
return redirect('/users')->with('success','Usuario actualizado correctamente.');
}
and this is the line of the database migration where the user type is:
$table->boolean('tipo_usuario')->default(0); //0 == editor && 1 == administrador
Finally after two days I managed to solve it, it was silly but since I'm new to this I didn't know, the mistake was that when dealing with numbers the value had to be a number and the rest was correctly set, so the select Option would remain: