投稿メソッドを使用してフォームを送信しようとしていますが、クエリ例外エラーが発生しています。laravelでajaxを使用してフォーム投稿を送信する方法5.4
SQLSTATE [23000]:整合性制約違反:1048列 'post_idの' がヌルにすることはできません(SQL:comments
(comment
、approved
、user_id
、post_id
、updated_at
、created_at
)値(sdfsdfsdff、1、1、挿入、2017年-12-04 07:18:13、2017-12-04 07:18:13))
sdfsdfsdffは「コメントシステム」を送信しようとしているコメントです.1はユーザーID、空白スペースは投稿IDがあるべき場所です。
これは私のコントローラのコードです:
$this->validate($request, [
'comment' => 'required'
]);
$post = Post::find($post_id);
$comment = new Comment();
$comment->comment = $request->comment;
$comment->approved = true;
$comment->user_id = auth()->id();
$comment->post_id = $request->id;
$comment->save();
return response()->json([
'comment' => $comment->comment,
'user_id' => $comment->user_id,
'post_id' => $comment->post_id,
'post_slug' => $post->slug,
'success' => true
]);
これが私の見解で私のAJAXコードです:
var urlPostComment = '{{ url('comments/store') }}';
$('.send').click(function(e){
e.preventDefault();
var dataSerialize = $('#comment-new').serialize();
$.ajax({
method: 'post',
url: urlPostComment,
data: dataSerialize,
dataType: 'json', // <- La coma
success: function (data) {
console.log(data); // <- Revisar JSON en la consola del navegador
if(data.success)
{
$('#comment-post').append(
'<p class="store-comment">' + data.comentario + '</p>'
);
}
}, // <- La coma
error: function() {
console.log('Error en el AJAX');
}, // <- La coma
complete: function() {
console.log('Termino el ajax');
}
});
});
マイフォーム:
<div class="row">
<div class="col-xs-8 col-md-offset-2 form-create">
{!! Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST', 'id' => 'add-comment']) !!}
<p class="error text-center alert alert-danger hidden"></p>
{{ Form::label('comment', ' Comentario', ['class' => 'glyphicon glyphicon-comment pull-right label-space', 'id' => 'comment-create']) }}
{{ Form::textarea('comment', null, ['class' => 'form-control comment-text', 'id' => 'comment-new', 'placeholder' => 'Escribe tu comentario']) }}
<p class="error text-center alert alert-danger hidden"></p>
{{ Form::submit('Agregar Comentario', ['class' => 'comment-message-guest-button send']) }}
{!! Form::close() !!}
</div>
</div>
、全コメントのhtml:
<div class="row">
<div class="col-md-12">
@foreach ($post->comments as $comment)
<section class="comment-list">
<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" class="store-comment" {{ $comment->id }} {{ $post->id }}>{{ $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>
</section>
@endforeach
</div>
</div>
0私は、私はAJAXを使用していないときに完全に働いた外部キーを持っています POST
http://localhost:8000/comments/store 500(内部サーバーエラー)「グーグルクロームコンソールから」
私のコメントテーブルでデータベース上:
メインエラー私はそれが関係問題だとは思わない。事前にありがとう
私はこの方法を使用する場合、私はこの取得:代わりに、私は「ストア」.. –