NoneNameDeveloper's avatar

Summernote image upload error while editing form | Laravel 5.7

I am using the Summernote editor to insert pictures with new data in the form. It does not make any mistakes while uploading new content to the Database; it works all the time. However, if I try to edit the content i have some errors. while entering a new image or if I want to add a new image to the content, there some following error is occurring.


The following error message will be displayed while editing the inserted information.
"Undefined offset: 1"

My Controller

public function addnew(Request $request)
    { 
        $data =  \Input::except(array('_token')) ;
        $rule=array(
                'menu_name' => 'required',            
                'lang_code' => 'required'
                 );
         $validator = \Validator::make($data,$rule);
        if ($validator->fails())
        {
                return redirect()->back()->withErrors($validator->messages());
        } 
        $inputs = $request->all();
        //dd($request->all());
        $ms = $inputs['news_description'];
    $dom = new \domdocument();
    $dom->loadHtml($ms, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    
    $images = $dom->getelementsbytagname('img');
    foreach($images as $k => $img){
        $datas = $img->getattribute('src');
        list($type, $datas) = explode(';', $datas);
        list(, $datas)      = explode(',', $datas);

        $datas = base64_decode($datas);
        $image_name= time().$k.'.png';
        $path = public_path() .'/upload/menus/'. $image_name;
        file_put_contents($path, $datas);

        $img->removeattribute('src');
        $img->setattribute('src', '/upload/menus/'.$image_name);
    }
    $ms = $dom->saveHTML();

    if(!empty($inputs['id'])) {
        $menus = Menus::findOrFail($inputs['id']);
    }else{
        $menus = new Menus;
    }
    $menu_slug = str_slug($inputs['menu_name'], "-");
    
    $menus->menu_name = $inputs['menu_name'];
    $menus->menu_slug = $menu_slug; 

    $menus->news_description = $ms;
    $menus->lang_code = $inputs['lang_code']; 
    $menus->save();
    if(!empty($inputs['id'])){
        \Session::flash('warning', "Changes saved");
        return \Redirect::back();
    }else{
        \Session::flash('warning', "The New Menu Added!");
        return \Redirect::back();
    }            
}   

public function editmenus($menu_id) { $menupost = Menus::findOrFail($menu_id return view('administrator.modules.addeditmenus',compact('menupost')); }

The problem here is that the content you edit should not be erroneously modified. How can I resolve the issue? Thanks!
0 likes
0 replies

Please or to participate in this conversation.