[英]How to delete a post together with image uploaded with summernote
The following is my store
and destroy
method respectively.下面分别是我的store
和destroy
方法。 Everything works as expected but when I delete a post it gets deleted but the image that is uploaded with the summer note doesn't.一切都按预期工作,但是当我删除帖子时,它会被删除,但与夏季笔记一起上传的图像却没有。 How can I go about achieving that?我怎样才能实现这一目标?
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required | max:100',
'category' => 'required',
'tags' => 'required',
'short_description' => 'required | max:200',
'image' => 'required | image',
'content' => 'required'
]);
$title = $request->title;
$category_id = $request->category;
$short_description = $request->short_description;
$mainImage = $request->file('image');
$content = $request->content;
$tags_id = $request->tags;
$user_id = Auth::user()->id;
$dom = new \DomDocument();
$dom->loadHtml($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imageFile = $dom->getElementsByTagName('img');
foreach ($imageFile as $item => $image) {
$data = $image->getAttribute('src');
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$imgeData = base64_decode($data);
$image_name = "/summernoteUploads/" . time() . $item . '.png';
$path = public_path() . $image_name;
file_put_contents($path, $imgeData);
$image->removeAttribute('src');
$image->setAttribute('src', $image_name);
}
$content = $dom->saveHTML();
$post = Post::create([
'title' => $title,
'category_id' => $category_id,
'short_description' => $short_description,
'content' => $content,
'user_id' => $user_id,
]);
$post->tags()->attach($tags_id);
$post->addMedia($mainImage)->toMediaCollection();
return redirect(route('posts.dashboard.index'))->with('status', 'Post created Successfully');
}
public function destroy(Post $post)
{
$post->delete();
return redirect()->back()->with('status', 'Post Deleted Successfully');
}
if you upload images in server and save the path in database do this function:如果您在服务器中上传图像并将路径保存在数据库中,请执行此 function:
public static function delete_image($image_path)
{
$image_path = public_path('uploads\\' . Str::replace('/', '\\', $image_path));
if (file_exists($image_path))
{
@unlink($image_path);
return true;
}
}
just send the path of image.只需发送图像的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.