一尘不染

错误500(内部服务器错误)ajax和laravel

ajax

大家好,我在laravel和ajax中使用我的评论系统时遇到问题。实际上,它仅适用于php,但我遇到了ajax问题。

这是错误消息:

状态码:500内部服务器错误。并且错误显示:1/3 SQLSTATE [23000]:违反完整性约束:1048列’post_id’不能为null。

我正在编辑模态中的注释,我可以创建一个新注释,但是问题是使用ajax编辑它。

JS代码:

<script>

  var commentId = 0;
        var divcomment = null;

        $('.edit-comment').click(function(event){
          event.preventDefault();
          var divcomment = this.parentNode.parentNode;
          commentId = $("#comment-post", event.target.parentNode.parentNode).data('commentid');
          var commentBody = $(divcomment).find('#display-comment').text();
          $('#comment').val(commentBody);
          $('#edit-comment').modal();
        });

        $('#modal-save').click(function(){
            $.ajax({
                method: 'POST',
                url: urlEdit,
                data: {
                    comment: $('#comment').val(),
                    commentId: commentId,
                    _token: token,
                    _method: 'POST'
                 }
            })
            .done(function (msg){
                $(divcomment).text(msg['new_comment']);
                $('#edit-comment').modal('hide');
            });
        });

</script>

这是HTML:

<article class="row">
    <div class="col-md-3 col-sm-3 hidden-xs">
        <figure class="thumbnail">
            <img class="img-responsive" src="/uploads/avatars/{{ $comment->user->profilepic  }}" />
            <figcaption class="text-center">{{ $comment->user->name }}</figcaption>
        </figure>
    </div>
    <div class="col-md-8 col-sm-8">
        <div class="panel panel-default arrow left">
            <div class="panel-body">
                <header class="text-left">
                    <div class="comment-user"><i class="fa fa-user"></i> {{ $comment->user->name }}</div>
                    <time class="comment-date" datetime="{{ $comment->created_at->diffForHumans() }}"><i class="fa fa-clock-o"></i> {{ $comment->created_at->diffForHumans() }}</time>
                </header>
                <div id="comment-post" data-commentid="{{ $comment->id }}">
                    <p id="display-comment">{{ $comment->comment }}</p>
                </div>
            </div>

            <div class="panel-footer list-inline comment-footer">
                @if(Auth::guest())

                    No puedes responder ningún comentario si no has ingresado.

                @else

                    @if(Auth::user() == $comment->user)
                        <a href="#" data-toggle="modal" data-target="edit-comment" class="edit-comment">Editar</a> <a href="#" data-toggle="modal" data-target="delete-comment" class="delete-comment">Eliminar</a>
                    @endif

                    @if(Auth::user() != $comment->user)
                        <a href="#">Responder</a>
                    @endif

                @endif
            </div>

        </div>
    </div>
</article>

我的编辑模式:

<div class="modal fade" id="edit-comment" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" style="color:#000;">Editar Comentario</h4>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="comment">Editar comentario</label>
            <textarea class="form-control" name="comment" id="comment"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn-comment-dismiss btn-comment-modal" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cerrar</button>
        <button type="button" class="btn-comment-edit btn-comment-modal" id="modal-save"><span class="glyphicon glyphicon-ok"></span> Editar</button>
      </div>
    </div>
  </div>
</div>

我的评论更新路线:

Route::POST('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);

我在CommentController上的更新功能:

public function update(Request $request)
{

    $this->validate($request, [
        'comment' => 'required'
    ]);

    $comment = Comment::find($request['commentId']);
    if (Auth::user() != $comment->user) {
        return redirect()->back();
    }

    $comment->comment = $request['comment'];

    $comment->update();
    return response()->json(['new_comment' => $comment->comment], 200);

}

最后,在“发布我的帖子”视图“我在其中显示评论”上创建的变量

<script>

  var token = '{{ Session::token() }}';
  var urlEdit = '{{ url('comments/update') }}';

</script>

更新:

评论表方案:

    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->text('comment');            
            $table->boolean('approved');
            $table->integer('post_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('comments', function ($table){
            $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
        });
    }

新更新:

错误消息:

误差1/3

SQLSTATE [23000]:违反完整性约束:1048列“ post_id”不能为空

错误2/3

SQLSTATE [23000]:违反完整性约束:1048列“ post_id”不能为空

错误3/3

SQLSTATE [23000]:完整性约束违规:1048列’POST_ID’不能为空(SQL:INSERT INTO
commentscommentapprovedpost_iduser_idupdated_atcreated_at)的值(另一个尚未评论,1,…,4,2017年6月4日4点54分34秒,2017-06-04
04:54:34))

附加信息:

一般

Request URL:http://devmedia.dev/comments/update
Request Method:POST
Status Code:500 Internal Server Error
Remote Address:127.0.0.1:80
Referrer Policy:no-referrer-when-downgrade

形成

comment:Another yet comment
commentId:13
_token:Do1gqYfziHij1nAj2CFOWwgdt7UWuubqbawrD5uX
_method:POST

整个评论路线:

Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
Route::get('comments/{id}/edit', ['uses' => 'CommentsController@edit', 'as' => 'comments.edit']);
Route::POST('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
Route::delete('comments/{id}', ['uses' => 'CommentsController@destroy', 'as' => 'comments.destroy']);
Route::get('comments/{id}/delete', ['uses' => 'CommentsController@delete', 'as' => 'comments.delete']);

阅读 249

收藏
2020-07-26

共1个答案

一尘不染

我(和安东)的预感似乎是正确的。您有两条冲突的路线。

Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);

而且当然

Route::post('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);

由于两条路线使用的路线大致相同,因此laravel首先经过定义的comments.store路线,这就是您的路线。

有几种方法可以解决此问题。

  1. 更改路线顺序:

    Route::post('comments/update', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
    

    Route::post(‘comments/{post_id}’, [‘uses’ => ‘CommentsController@store’, ‘as’ => ‘comments.store’]);
    Route::get(‘comments/{id}/edit’, [‘uses’ => ‘CommentsController@edit’, ‘as’ => ‘comments.edit’]);

  2. 使用路线约束:

    Route::post('comments/{post_id}', [
    'uses' => 'CommentsController@store',
     'as' => 'comments.store'
    

    ])->where([‘post_id’ => ‘[0-9]+’]);;
    Route::get(‘comments/{id}/edit’, [‘uses’ => ‘CommentsController@edit’, ‘as’ => ‘comments.edit’]);
    Route::post(‘comments/update’, [‘uses’ => ‘CommentsController@update’, ‘as’ => ‘comments.update’]);

值得注意的是,我不知道Facade注册表如何处理外观方法的大小写(上下左右)。因此,为了不引起其他错误,我使用的下大写字母POST,就像在文档中一样。

2020-07-26