zanko
zanko

Reputation: 41

i write the below code , how can i cleaning it? can any one help me to write it better?

I write this code in laravel any one can help me to write it better ?

 Setting::where('key' , 'online_course')->update(['value' => $request->get('online_course') ]);
    Setting::where('key' , 'read_item')->update(['value' => $request->get('read_item') ]);
    Setting::where('key' , 'comment_item')->update(['value' => $request->get('comment_item') ]);
    Setting::where('key' , 'rate_item')->update(['value' => $request->get('rate_item') ]);

Upvotes: 0

Views: 61

Answers (2)

zanko
zanko

Reputation: 41

with your idea i write this code

foreach ($request->except('_token' , '_method') as $key => $value){
        Setting::where('key' , $key)->update(['value' => $value]);
    }

but this code working correctly

 foreach ($request->all() as $key => $value){
        Setting::where('key' , $key)->update(['value' => $value]);
    }

Upvotes: 1

Peppermintology
Peppermintology

Reputation: 10210

A possible solution would be to use a foreach loop pulling your keys and values from the $request object:

foreach ($request->only('online_course', 'read_item', 'comment_item', 'rate_item') as $key => $value) {
    Setting::where('key', $key)->update(['value', $value]);
}

You could use $request->all() to grab all the keys from the $request object to shorten it further but if you were to do that, ensure that you have sufficient valudation and safe guards in place (such as mass assignment) so that you don't accidentally use some unwanted $request data.

Upvotes: 3

Related Questions