gpt4 book ai didi

laravel - 更新数据库中的列时出错 - 违反完整性约束 : Column 'xxxxxxxx' cannot be null

转载 作者:行者123 更新时间:2023-12-05 04:41:18 25 4
gpt4 key购买 nike

我是这个主题的新手,我有一个从数据库中的表中获取数据的下拉列表,当我选择另一个选项并保存时,给我:违反完整性约束: 1048 列 'department_id' 不能为空(SQL:更新 users 设置 department_id = 2021-11-25 09:41:43 其中 id = 1)

我的索引:

<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>

我的 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();
}

用户模型:


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;
}

要更新的列是 department_id,它是 User 表中的外键

最佳答案

在您的 User 模型中,确保您的 $fillable 属性具有正确的属性:

class User extends Authenticatable
{
protected $fillable = ['name','email','department_id','role'];
....
}

关于laravel - 更新数据库中的列时出错 - 违反完整性约束 : Column 'xxxxxxxx' cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70108369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com