我建立了一个具有公告/博客功能的网站,但还没有任何删除/编辑功能,我尝试了几件事,但是它们要么无法正常工作,要么根本无法工作。
我尝试使用下拉的WYSIWYG编辑器编辑帖子/评论,如果您单击编辑按钮,它将使用Jquery扩展评论,然后用户可以编辑他们的评论。问题在于,这仅适用于最上面的注释,因为每个编辑div / form具有相同的ID。
删除存在问题,即注释连接到具有外键约束的帖子,因此,如果我尝试删除帖子,由于外键,它不会让我失望。
//删除功能
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());
}
///查看评论并编辑表格/所见即所得
<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中起作用,因此用户可以编辑每个注释。
在此先感谢您,很长的问题很抱歉:)
答案 0 :(得分:1)
这与Laravel或应用程序设计无关,您需要通知SGBD(Mysql),当删除某个表上的记录时,与被删除的表相关的所有记录也需要删除。可以通过在迁移中使用方法onDelete('cascade')
来实现此功能。
https://laravel.com/docs/5.8/migrations#foreign-key-constraints
Schema::table('replies', function (Blueprint $table) {
$table->autoIncrement('id');
....
$table->foreign('announcement_id')->references('id')->on('announcements')->onDelete('cascade');
});
通过这种方式,您无需在代码中包含此部分:
$replies = Reply::where('post_id', $announcement->id);
$announcements->delete($replies, $announcements);
我建议您也检查laravel中雄辩的关系,这使得管理表之间的关系更加容易。
https://laravel.com/docs/5.8/eloquent-relationships
例如,如果您想选择单个通告的所有答复,则只需要编写
$annoucement->replies();