Итак, я пытаюсь создать динамический раздел комментариев, но эта проблема, похоже, беспокоит меня. 500 Internal Server Error.Я немного читал об этой проблеме, но не смог ее исправить.Я использовал некоторые фрагменты кода, чтобы найти проблему, но я до сих пор не могу найти, в чем проблема.
create.blade.php -
<h1>Create comment</h1>
{!! Form::open(['id' => 'myForm', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group">
{{ Form::textarea('body', '', ['class' => 'form-control', 'id' => 'article-ckeditor', 'placeholder' => 'Share your thoughts related to this post']) }}
</div>
{{ Form::hidden('post_id', $post->id) }}
{{Form::submit('Submit', ['class' => 'btn btn-primary', 'id' => 'submitComment'])}}
{!! Form::close() !!}
Ajax -
$(document).ready(function(){
$('#submitComment').click(function(e){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: "{{ url('/comments') }}",
method: 'POST',
data: {
body: $('#article-ckeditor').val(),
post_id: $('#post_id').val()
},
success: function(result){
$('.alert').show();
$('.alert').html(result.success);
},
/* error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}*/
error: function(jqXhr, json, errorThrown){// this are default for ajax errors
var errors = jqXhr.responseJSON;
var errorsHtml = '';
$.each(errors['errors'], function (index, value) {
errorsHtml += '<ul class="list-group"><li class="list-group-item alert alert-danger">' + value + '</li></ul>';
});
//I use SweetAlert2 for this
swal({
title: "Error " + jqXhr.status + ': ' + errorThrown,// this will output "Error 422: Unprocessable Entity"
html: errorsHtml,
width: 'auto',
confirmButtonText: 'Try again',
cancelButtonText: 'Cancel',
confirmButtonClass: 'btn',
cancelButtonClass: 'cancel-class',
showCancelButton: true,
closeOnConfirm: true,
closeOnCancel: true,
type: 'error'
}, function(isConfirm) {
if (isConfirm) {
$('#openModal').click();//this is when the form is in a modal
}
});
}
});
});
});
Маршрут -
Route::resource('/comments', 'CommentsController');
КомментарииКонтроллер @ store -
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'body' => 'required'
]);
if ($validator->fails()) {
if($request->ajax())
{
return response()->json(array(
'success' => false,
'message' => 'There are incorect values in the form!',
'errors' => $validator->getMessageBag()->toArray()
), 422);
}
$this->throwValidationException(
$request, $validator
);
}
// Create Post
$comment = new Comment;
$comment->body = $request->body;
$comment->user_name = auth()->user()->name;
$comment->post_id = $request->post_id;
$comment->profile_picture = auth()->user()->profile_picture;
$comment->save();
return response()->json(['success'=>'Data is successfully added']);
//return redirect()->back()->with('success', 'Comment Created');