zunkt's avatar
Level 2

Set Value For Key in Database

Hi everyone! Today i'm doing about my project and i make a update to load Image and store it in DB. But i don't know how can i set value for key. Here is my Controller:

public function update(Request $request, Setting $setting)
    {
        if($request->hasFile('site_logo')){
            $filenameWithExtLogo = $request->file('site_logo')->getClientOriginalName();
            $filenameLogo = pathinfo($filenameWithExtLogo, PATHINFO_FILENAME);
            $extLogo = $request->file(('site_logo'))->getClientOriginalExtension();
            $filenameToStoreLogo = $filenameLogo.'_'.time().'.'.$extLogo;
            $pathLogo = $request->file('site_logo')->storeAs('public/image_site', $filenameToStoreLogo);
            Config::set('setting.site_logo', $pathLogo);
        }
        if($request->hasFile('site_favicon')){
            $filenameWithExtFavicon = $request->file('site_favicon')->getClientOriginalName();
            $filenameFavicon = pathinfo($filenameWithExtFavicon, PATHINFO_FILENAME);
            $extFavicon = $request->file(('site_favicon'))->getClientOriginalExtension();
            $filenameToStoreFavicon = $filenameFavicon.'_'.time().'.'.$extFavicon;
            $pathFavicon = $request->file('site_favicon')->storeAs('public/image_site', $filenameToStoreFavicon);
            Config::set('setting.site_favicon', $pathFavicon);
        }
        $setting->value = $request->get('value');
        $setting->save();
        return 'Upload Sussces';
    }

If i use Config to set it don't happen anything in database. So how can i set up it on DB. Key: site_logo with value. site_favicon with value. Please help me. Thank so much

0 likes
4 replies
automica's avatar

@zunkt have you added the site_logo field to your $fillable array in your model? if not, you wont be able to write the value to the db.

zunkt's avatar
Level 2

@automica hi it's a fill key in database. And site_logo is a one of key in it. I want store my path to site_logo like this:

id key           value
6 site_logo   abcxyz.jpg
automica's avatar

@zunkt ah, providing you've got fillable array with key and value in its you should be able to amend your code to add the setting.key as follows

$setting->key = 'site_logo';
$setting->value = $request->get('value');
$setting->save();

Please or to participate in this conversation.