简体   繁体   中英

How can I update an exiting column on Laravel

I have a page where I am able to add content using a summernote.But when I update the content there are no changes.

Here is my StoreController.

public function store(Request $request)
{
    $detail=$request->content;
    $dom = new DOMDocument();
    $dom->loadHtml($detail, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $images = $dom->getelementsbytagname('img');
    foreach($images as $k => $img){
        $data = $img->getattribute('src');
        list($type, $data) = explode(';', $data);
        list(, $data)      = explode(',', $data);
        $data = base64_decode($data);
        $image_name= time().$k.'.png';
        $path = public_path('uploads') .'/'. $image_name;

        file_put_contents($path, $data);

        $img->removeattribute('src');
        $img->setattribute('src', 'uploads'.'/'.$image_name);
    }

    $detail = $dom->savehtml();
    $abouts = new about;
    $abouts->content = $detail;
    $abouts->save();
    return view('admin.about',compact('abouts'));
}

Here is my UpdateController

    public function update(Request $request)
    {
        if($request->isMethod('POST')){

            //in this section all code are same as like as my store controller
            
            $abouts->update();
            return view('admin.about',compact('abouts'));
        }

    }

Any ideas on how I could fix this?

In your Store function you have used this $abouts = new about; Are you doing the same in update functions? then it will be a new instance.

Adding Validations may help you know the reason why it's not getting updated.

\\Log::debug('message'); can also be useful if you still don't get the issue after putting validations.

public function update($id, Request $request)
{
    //you can pass $id of the record which you want to update or even pass it along with the other request parameters.
    $task = Task::findOrFail($id);

    //validate the request( would suggest to do the same in your store function too. )
    $this->validate($request, [
        'title' => 'required',
        'description' => 'required'
    ]);

    $input = $request->all();

    $task->fill($input)->save();

    Session::flash('flash_message', 'Task successfully added!');

    return redirect()->back();
}

The best solution is to test it first if you are getting data in the request or not using dd() method. OR

you can try this:

public function update(Request $request)
{
    if($request->isMethod('PUT')){

        //in this section all code are same as like as my store controller
        
        $abouts->update();
        return view('admin.about',compact('abouts'));
    }

}

or you can remove that if condition and try

public function update(Request $request)
{
    

        //in this section all code are same as like as my store controller
        
        $abouts->update();
        return view('admin.about',compact('abouts'));
    

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM