簡體   English   中英

Laravel 更新數據庫中的鍵和值

[英]Laravel update key and value in database

我在 db 中有鍵和值列,如何從 controller 更新它們?

我如何通過密鑰更新

$settings = \App\Setting::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"
    },
 }

好吧,如果你做一些類似的事情,

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

對於可能偶然發現這一點並需要幫助的人,我發現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