500 (Internal Server Error) ajax and laravel

Viewed 2922

Hey everyone i'm having an issue editing comments with ajax in laravel 5.4

This works in a modal "press edit and modal opens" so the problem is when y click on edit "editar" i get the 500 (Internal Server Error)

First of all i will show you my js code:

$(document).ready(function(){

var commentId = 0;
    var divcomment = null;

    $('.edit-comment').click(function(event){
      event.preventDefault();
      var divcomment = this.parentNode.parentNode;
      commentId = event.target.parentNode.parentNode.dataset['commentid'];
      var commentBody = $(divcomment).find('#display-comment').text();
      $('#comment-body').val(commentBody);
      $('#edit-comment').modal();
    });

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

});

My comments controller, update function:

    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);

}

I make a var for url: urlEdit, on my view so here's my view:

<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>

This is the modal:

<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>

of course that is inside of a foreach loop.

I made 2 vars on my view:

    <script>
    $(document).ready(function(){
     var token = '{{ Session::token() }}';
     var urlEdit = '{{ url('comments/update') }}';
    });
    </script>

And finally a screenshot:
Screenshot

Thank you in advance.

3 Answers
Related