简体   繁体   English

如何更新 Laravel 上的现有列

[英]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.我有一个页面,我可以在其中使用 Summernote 添加内容。但是当我更新内容时,没有任何变化。

Here is my StoreController.这是我的 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这是我的 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;在您的 Store 函数中,您使用了$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.如果您在请求中获取数据或未使用dd()方法,则最好的解决方案是首先对其进行测试。 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或者你可以删除它 if 条件并尝试

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'));
    

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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