简体   繁体   English

Laravel 更新数据库中的键和值

[英]Laravel update key and value in database

i have key and value columns in db how can i update them from controller?我在 db 中有键和值列,如何从 controller 更新它们?

How Can i update by key我如何通过密钥更新

$settings = \App\Setting::where('type','synrisk')->get();
$settings->fill($request->all());
$settings->save();
Session::flash('message', 'تم التعديل بنجاح');
return redirect()->route('about_us_admin');

My Form response我的表格回复

 _token: "zLiFs10lNkuIZxu2DPyPwetsA3HCaNlgLb9L1w45",
 full_name: "Mazen",
 email: "mazen@aramex.com",

My db我的数据库

 {
    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"
    },
 }

Well if you do some thing like,好吧,如果你做一些类似的事情,

  $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();

For someone who may stumble upon this and needs help, I found foreach useful in this case senario, Good Luck对于可能偶然发现这一点并需要帮助的人,我发现foreach在这种情况下很有用,祝你好运

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM