【发布时间】:2019-11-09 11:30:03
【问题描述】:
我建立了一个带有公告/博客端的网站,还没有任何删除/编辑功能,我尝试了几件事,但它们要么不能正常工作,要么根本不能正常工作。
我尝试使用下拉所见即所得编辑器编辑帖子/评论,如果您单击编辑按钮,它将使用 Jquery 展开评论,然后用户可以编辑他们的评论。问题是这仅适用于顶部评论,因为每个编辑 div/form 具有相同的 ID。
删除的问题是 cmets 连接到具有外键约束的帖子,所以如果我尝试删除帖子,它不会因为外键而让我删除。
//删除函数
public function deleteannouncement(Announcement $announcement, $id){
$announcements = Announcement::findOrFail($id);
$replies = Reply::where('post_id', $announcement->id);
$announcements->delete($replies, $announcements);
return redirect('/mededelingen/')->with('success','Bericht succesvol verwijderd!');
}
public function deletereply($id){
$replies = Reply::findOrFail($id);
$replies->delete($replies);
return redirect(url()->previous())->with('success','Opmerking succesvol verwijderd!');
}
//编辑函数评论/回复
public function postSummernoteeditorReply(Request $request, $id){
$this->validate($request, [
'detail' => 'required',
]);
$detail=$request->detail;
$dom = new \DomDocument();
$dom->loadHtml( mb_convert_encoding($detail, 'HTML-ENTITIES', "UTF-8"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$images = $dom->getElementsByTagName('img');
foreach($images as $img){
$src = $img->getAttribute('src');
// if the img source is 'data-url'
if(preg_match('/data:image/', $src)){
// get the mimetype
preg_match('/data:image\/(?<mime>.*?)\;/', $src, $groups);
$mimetype = $groups['mime'];
// Generating a random filename
$filename = uniqid();
$filepath = "/img/blog/$filename.$mimetype";
// @see http://image.intervention.io/api/
$image = Image::make($src)
// resize if required
/* ->resize(300, 200) */
->encode($mimetype, 100) // encode file to the specified mimetype
->save(public_path($filepath));
$new_src = asset($filepath);
$img->removeAttribute('src');
$img->setAttribute('src', $new_src);
} // <!--endif
} // <!--endforeach
$detail = $dom->saveHTML();
$summernote = Summernote::find($id);
$summernote->post_content = $detail;
//dd($summernote->post_content);
//dd($summernote->post_id);
$summernote->update();
return redirect(url()->previous());
}
//使用cmets查看和编辑表单/所见即所得
<div class="card-body">
@foreach($replies as $reply)
<div class="announcement">
@if(Auth::user()->admin == 1 || Auth::user()->id == $reply->user_id)
<a href="/reactie/destroy/{{$reply->id}}" class="float-right"><i class="fal fa-dumpster"></i></a>
@endif
@if(Auth::user()->id == $reply->user_id)
<i class="fal fa-pencil float-right" id="yeet" class="float-right" style="color: #007ac3; margin-right: 10px;"></i>
@endif
<p style="font-size: 0.8rem;">{{date("j F Y", strtotime($reply->created_at))}} | Geplaatst door <span>{{$reply->username}}</span></p>
<p style="margin-top: -10px;">{!! $reply->post_content !!}</p>
@if(Auth::user()->id == $reply->user_id)
<div class="reply-expand" style="display: none;">
<form method="POST" action="{{ route('Reply Edit', ['id' => $reply->id]) }}">
@csrf
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Reactie Aanpassen:</strong>
<textarea class="form-control summernote" name="detail">{!! $reply->post_content !!}</textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary pull-right" style="border-radius: 0px; box-shadow: 0px 1px 10px -4px #000000;">Aanpassen</button>
</div>
</form>
</div>
@endif
<hr>
</div>
@endforeach
{{ $replies->links() }}
<form method="POST" action="{{ route('editor.post', ['id' => $announcements->id]) }}">
@csrf
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Reactie:</strong>
<textarea class="form-control summernote" name="detail"></textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary pull-right" style="border-radius: 0px; box-shadow: 0px 1px 10px -4px #000000;">Reageer</button>
</div>
</form>
</div>
//所见即所得和我的编辑切换的Jquery
$(document).ready(function() {
$('.summernote').summernote({
height: 400,
});
$('#yeet').click(function() {
$('.reply-expand').toggle("slide");
});
});
我希望实现的是让管理员和 OP 能够在没有外键约束问题的情况下删除他们的帖子。如果帖子被删除,则需要将其删除。
编辑功能在我的@foreach 中运行,因此用户可以编辑每条评论。
提前谢谢你,很抱歉这个冗长的问题:)
【问题讨论】:
标签: javascript jquery laravel