>我在数据库中有键和值列,如何从控制器更新它们?
如何按键更新
$settings = AppSetting::where('type','synrisk')->get();
$settings->fill($request->all());
$settings->save();
Session::flash('message', 'تم التعديل بنجاح');
return redirect()->route('about_us_admin');
我的表单响应
_token: "zLiFs10lNkuIZxu2DPyPwetsA3HCaNlgLb9L1w45",
full_name: "Mazen",
email: "mazen@aramex.com",
我的数据库
{
id: 4,
key: "phone_number_1",
value: "07777777",
type: "Aramex",
created_at: "2019-06-16 12:48:43",
updated_at: "2019-06-16 12:48:43"
},
{
id: 5,
key: "phone_number_2",
value: "07777777",
type: "Aramex",
created_at: "2019-06-16 12:48:43",
updated_at: "2019-06-16 12:48:43"
},
}
对于可能偶然发现并需要帮助的人,我发现在这种情况下foreach
有用的 senario, 祝你好运
foreach ($request->all() as $key => $value) {
if ($request->hasFile($key)) {
$value = Storage::disk('public')->put('settings', $request->file($key));
$setting->where('key', $key)->update(['value' => $value]);
} else {
$setting->where('key', $key)->update(['value' => $value]);
}
}
好吧,如果你做一些类似的事情,
$fullName = request()->only('full_name')
$fullNameKeyValue = [
'key' => array_key_first($fullName),
'value' => array_key_first(array_flip($fullName))
];
// You can extract this logic to a function and have checks in place as well
// like is_array() and array_key_exists() .
$settings->fill($fullNameKeyValue);
$settings->save();