funfelfonfafis funfelfonfafis - 11 months ago 43
PHP Question

Laravel 5. Dropdown doesn't change database

I have a problem. I added dropdown to form, but He only shows the list. When I click on buttun "send" my database doesn't change.

Form::text
change my database, but
Form::select
doesn't make any changes. May, i have a problem in the controller?

Controller

public function edit($id)
{
$user = User::with('roles')->findOrFail($id);
$userRole = Role::pluck('name', 'id');
return view('pages.edit', compact('user', 'userRole'));
}

public function update($id, Request $request)
{
$user = User::with('roles')->findOrFail($id);
$user->update($request->all());
return redirect('users');
}


View

{!! Form::model($user, ['method' => 'PATCH', 'action'=>['UsersController@update', $user->id]]) !!}
<div class="form-group">
<div class="form-group col-md-offset-3 col-md-6">
{!! Form::label('name','Name: ') !!}
{!! Form::text('name', null, ['class'=>'form-control','placeholder'=>'Here, user name']) !!}
</div>

<div class="form-group col-md-offset-3 col-md-6">
{!! Form::label('roles','Roles: ') !!}
{!! Form::select('roles',$userRole, null,['class'=>'form-control']) !!}
</div>

<div class="form-group col-md-offset-3 col-md-6">
{!! Form::submit('Send', ['class'=>'btn btn-success form-control']) !!}
</div>
</div>
{!! Form::close() !!}


Database

Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
...
});
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->primary(['role_id', 'user_id']);
});

Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
...
});


User Model

class User extends Authenticatable
{
use Notifiable, HasRoles;

protected $table = 'users';

protected $fillable = [
'name', 'email', 'password',
];

protected $hidden = [
'password', 'remember_token',
];

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


Role Model

class Role extends Model{
public function permissions(){}
public function givePermissionTo(Permission $permission){}
}


Has Role //not my code

trait HasRoles{

public function roles()
{
return $this->belongsToMany(Role::class);
}

public function assignRole($role)
{
return $this->roles()->save(
Role::whereName($role)->firstOrFail()
);
}

public function hasRole($role)
{
if (is_string($role)) {
return $this->roles->contains('name', $role);
}

return !! $role->intersect($this->roles)->count();
}

public function hasPermission(Permission $permission)[]
}


Thx for help.

Answer Source

Since Roles is a relationship you need to attach or associate the roles to your user. Depending on the type of relationship.

You will have to attach the role_id to your user model, add this in your controller:

$user->roles()->attach($request->roles);
$user->save();

As noted in the comments, the keys on your drop-down fields are the id's of the roles.

ps: these are the basics of laravel eloquent, so i would suggets to read the documentation first. https://laravel.com/docs/5.4/eloquent-relationships

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download