【问题标题】:How can I let users edit and delete posts and comments in a selfmade blog?如何让用户在自制博客中编辑和删除帖子和评论?
【发布时间】: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


    【解决方案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 中的 eloquent 关系,它使管理表之间的关系变得更加容易。

    https://laravel.com/docs/5.8/eloquent-relationships

    例如,如果你想选择单个公告的所有回复,你只需要写

    $annoucement->replies();
    

    【讨论】:

    • 谢谢,这帮助我删除了包含所有 cmets/回复的公告,现在我遇到了只能编辑第一个回复的问题。你对我有什么建议吗?
    猜你喜欢
    • 2019-02-20
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2013-09-03
    • 1970-01-01
    • 2021-01-18
    • 2011-03-30
    相关资源
    最近更新 更多