大家好,我在 laravel 和 ajax 中的评论系统遇到问题。实际上它只适用于 php,但我在使用 ajax 时遇到了问题。
这是错误消息:
Status Code:500 Internal Server Error. And the error says: 1/3 SQLSTATE[23000]:Integrity constraint violation:1048 Column 'post_id' cannot be 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">×</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']);
我在 CommentsController 上的更新功能:
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);
}
最后是在我的帖子单一 View “我显示评论的位置”上创建的变量
<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]: Integrity constraint violation: 1048 Column 'post_id' cannot be null
错误2/3
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null
错误3/3
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null (SQL: insert into
comments
(comment
,approved
,post_id
,user_id
,updated_at
,created_at
) values (Another yet comment, 1, , 4, 2017-06-04 04: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']);
最佳答案
看来我(和Anton's)的预感是正确的。您有两条相互冲突的路线。
Route::post('comments/{post_id}', ['uses' => 'CommentsController@store', 'as' => 'comments.store']);
当然
Route::post('comments/', ['uses' => 'CommentsController@update', 'as' => 'comments.update']);
因为这两个路由使用大致相同的路由,所以 Laravel 只是采用先定义的路由,即您的 comments.store
路由。
有几种方法可以解决这个问题。
更改路线的顺序:
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']);
使用路线约束:
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 注册器如何处理 Facade 方法的大小写(下、上)。因此,为了不引起进一步的错误,我使用了 POST
,就像文档中使用的那样。
关于javascript - 错误 500(内部服务器错误)ajax 和 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44350927/