简体   繁体   English

Laravel 5.7向数据库添加注释

[英]Laravel 5.7 add comments to database

I am following tutorial for creating forum in Laravel and I am currently at posts replies so I can add them to the database. 我正在关注在Laravel中创建论坛的教程,目前我在帖子回复中,因此可以将其添加到数据库中。

I have this function in ForumController.php 我在ForumController.php中有此功能

public function postReply(CreateReplyRequest $request) {

    $post =  Post::where('slug', '=', $request['slug'])->first();

    if( $post ) {
        $reply = new Reply;

        $reply->post_id = $post->id;
        $reply->user_id = Auth::user()->id;
        $reply->text = $request['text'];

        $reply->save();

        return redirect()->back();
    }
    return redirect('/');
}

It is only returning me back to homepage (/) and not adding any reply to database. 它只会使我返回首页(/),而不会在数据库中添加任何回复。 I do not know where can be error. 我不知道哪里可能出错。 Thanks for help. 感谢帮助。

Try this: 尝试这个:

 public function postReply(CreateReplyRequest $request) {

    $post =  Post::where('slug', $request['slug'])->first();

    if( $post != null) {
        $reply = new Reply;

        $reply->post_id = $post->id;
        $reply->user_id = Auth::user()->id;
        $reply->text = $request['text'];

        $reply->save();

        return redirect()->back();
    }
}

Thanks for answers. 感谢您的回答。 I had to install Form package from Laravel Collective via Composer. 我必须通过Composer从Laravel Collective安装Form软件包。 I had to pass the slug via {!! Form::hidden('slug', $post->slug) !!} 我不得不通过{!! Form::hidden('slug', $post->slug) !!} {!! Form::hidden('slug', $post->slug) !!} . {!! Form::hidden('slug', $post->slug) !!} Thanks for help. 感谢帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM