大家好,我在laravel和ajax中的评论系统遇到问题。实际上它只适用于PHP,但我遇到了ajax的问题。
这是错误消息:
状态代码:500内部服务器错误。错误说明:1/3 SQLSTATE [23000]:完整性约束违规:1048列'post_id'不能为空。
我正在编辑模式中的注释,我可以创建一个新注释,但问题是用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);
}
最后在我的Post单视图中创建的变量“我在哪里显示评论”
<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:插入
comments
(comment
,approved
,post_id
,{ {1}},user_id
,updated_at
)值(另一个评论,1,4,2017-06-04 04:54:34,2017-06-04 04:54:34) )
其他信息:
常规
created_at
表格
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
答案 0 :(得分:3)
看来我(和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注册商如何处理外观方法的外壳(下部,上部)。所以为了不引起进一步的错误,我使用了{{1}的下部外壳就像在文档中使用一样。