问题1:无法更新关系表
profiles
的记录。最终出现以下错误。
Illuminate \ Database \ QueryExceptionSQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ user_id”(SQL:更新
users
设置为user_id
= 132,users
。updated_at
= 2020-03-30 08:48 :51其中id
= 132)
我有两个具有以下架构的表users
和profiles
Schema::create(
'users',
function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->enum('role', ['super', 'admin', 'manager', 'subscriber', 'user'])->default('user');
$table->boolean('is_root')->default(FALSE);
$table->rememberToken();
$table->timestamps();
$table->unique(['username', 'email'], 'users_unique_credentials');
}
);
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->string('key', 191);
$table->longText('value')->nullable();
$table->foreign('user_id', 'profile_uid_fk')
->references('id')
->on('users')
->onDelete('cascade');
$table->unique(['user_id', 'key'], 'profile_unique_key');
});
public function profiles()
{
return $this->hasMany(Profile::class)->orderBy('id');
}
public function user()
{
return $this->belongsTo(User::class);
}
public function update(UserRequest $request, User $user)
{
$user->email = $request->email;
$user->role = $request->role;
if ($request->has('password')) {
$user->password = Hash::make($request->password);
}
$user->save();
$user->profile()->save($user);
}
问题2:
user_id
是表单中的隐藏字段。但是,我宁愿从控制器传递它。有没有方式吗?
array:15 [▼
"_token" => "O3Ardvzz7QvAsYa7aUWn4dbJx1qpCsScykD1fh1S"
"_method" => "PUT"
"email" => "newage@test.com"
"password" => null
"password_confirmation" => null
"role" => "subscriber"
"first_name" => "John"
"last_name" => "doe"
"city" => "Ahmedabad"
"mobile" => "545466555"
"facebook" => "https://facebook.com/profile/pp"
"twitter" => "https://twitter.com"
"youtube" => null
"instagram" => null
"user_id" => "132"
]