I'm new to the subject, I have a dropdown that fetches data from a table in the database, when I select another option and save, give me: Integrity constraint violation: 1048 Column 'department_id' cannot be null (SQL: update users set department_id = ?, users.updated_at = 2021-11-25 09:41:43 where id = 1)
My Index:
<div class="form-group mb-6">
<label class="form-label">{{ $trans('labels.department') }}</label>
<select class="form-select" v-model="form.department_id">
<option :value="department.id" v-for="department in $page.departments">
<p class="mt-1 text-sm leading-5 text-gray-500">{{ department.name }}</p>
</option>
</select>
</div>
My Controller:
public function update(Request $request)
{
$this->validate($request, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,' . auth()->user()->id]
]);
auth()->user()->update([
'name' => $request->input('name'),
'email' => $request->input('email'),
'department_id' => $request->input('department_id'),
]);
if ($request->hasFile('profile_picture')) {
$media = MediaUploader::fromSource($request->file('profile_picture'))
->toDestination('public', 'avatars')
->onDuplicateIncrement()
->useHashForFilename()
->beforeSave(function ($media) {
$media->uuid = Str::uuid();
})
->upload();
auth()->user()->attachMedia($media, 'avatars');
}
session()->flash('message', __('app.messages.profile-updated'));
return back();
}
USER Model:
protected $hidden = [
'password', 'remember_token',
];
protected static function boot()
{
parent::boot();
static::creating(function ($user) {
$user->uuid = Str::uuid();
});
}
public function tenant()
{
return $this->belongsTo(Tenant::class);
}
public function department()
{
return $this->belongsToMany(Department::class);
}
public function favoriteProjects()
{
return $this>belongsToMany(Project::class,'favorite_project_user');
}
public function isAdmin()
{
return $this->role === User::ROLE_ADMIN;}
public function isTenantUser()
{
return $this->role === User::ROLE_TENANT_USER;
}
public function cacheKey($key)
{
return auth()->user()->uuid . '.' . $key;
}
The column to update is department_id which is a foreignKey in User table