[英]How can I update an exiting column on Laravel
我有一个页面,我可以在其中使用 Summernote 添加内容。但是当我更新内容时,没有任何变化。
这是我的 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'));
}
这是我的 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'));
}
}
关于如何解决这个问题的任何想法?
在您的 Store 函数中,您使用了$abouts = new about;
你在更新功能中做同样的事情吗? 那么它将是一个新实例。
添加验证可能会帮助您了解未更新的原因。
\\Log::debug('message');
如果您在进行验证后仍然没有遇到问题,这也很有用。
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();
}
如果您在请求中获取数据或未使用dd()
方法,则最好的解决方案是首先对其进行测试。 或者
你可以试试这个:
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'));
}
}
或者你可以删除它 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.