[英]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.