I'm using Laravel 8 for my project, and in this project, I want to apply OneToMany relationship between users and roles tables.
So at the Models, I've added these:
Role.php:
public function users()
{
return $this->hasMany(Role::class);
}
User.php:
public function role()
{
return $this->belongsTo(User::class);
}
Then for creating new users, I've added this form field:
<div class="form-group">
<label class="col-sm-2 control-label">Role</label>
<select class="form-control" name="role" dir="ltr">
@foreach(\App\Models\Role::all() as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
@endforeach
</select>
</div>
And at the Store method, I coded this:
public function store(Request $request)
{
$validate_data = Validator::make(request()->all(),[
'name' => 'required|min:4',
'email' => 'required|email|unique:users',
'status' => 'required',
'role' => 'required',
'password' => 'required|min:6'
])->validated();
$creation = User::create([
'name' => $validate_data['name'],
'email' => $validate_data['email'],
'status' => $validate_data['status'],
'role_id' => $validate_data['role'],
'password' => bcrypt($validate_data['password'])
]);
if($creation){
return redirect()->back();
}else{
return '1003';
}
}
But now the problem is, whenever I try to add a new user, it will be added successfully to the table users but the column role_id will remain empty:
And when I say dd($request); it prints this:
As you can see it stores the role but I don't know why it does not save it in the database!
So what is going wrong here? How can I fix this issue?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.

