-1

i have key and value columns in db how can i update them from 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"
    },
 }
Erkan Özkök
  • 895
  • 12
  • 25
  • Can you explain a bit more ?? You are only getting full name and email coming through in request... what do you mean key and value ?? – alithedeveloper Jun 16 '19 at 12:36
  • @alithedeveloper i'm saving the type of value like email in key and the email (example@gmail.com) in value – Kareem Elsharkawy Jun 16 '19 at 12:38
  • I didn't understand your DB structure. Can you add some more examples. Where is email? Where is full_name? Will multiple row update for every request? – Erkan Özkök Jun 16 '19 at 17:55

2 Answers2

0

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();
alithedeveloper
  • 687
  • 5
  • 17
0

For someone who may stumble upon this and needs help, I found foreach useful in this case senario, Good Luck

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]);
            }
            
        }
James Christian Kaguo
  • 1,251
  • 15
  • 14