こんにちは、私はajaxでCRUDをやっていますが、私は店のコメントに問題があります。 は、私はこのエラーがあります:Laravel 5.1 - ajaxでクルー
QueryException in Connection.php line 655: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into
comments
(content
,user_id
,product_id
,article_id
,updated_at
,created_at
) values (my comment, , , , 2016-06-20 10:37:57, 2016-06-20 10:37:57))
私は、テキスト「私のコメント」とのコメントを保存するためにしようと試みました。私の入力 "コンテンツ"は私のコントローラに渡されますが、 "article_id"、 "user_id"、 "product_id"のような隠れた入力は私のコントローラに渡されません。
CommentController:
public function store(Request $request)
{
if($request->ajax()){
$comment = new Comment();
$comment->content = $request->input('content');
$comment->user_id = $request->input('user_id');
$comment->product_id = $request->input('product_id');
$comment->article_id = $request->input('article_id');
$comment->save();
return response()->json([
"message" => "Comment pubblished!"
]);
}
}
フォームコメント記事:へ
$("#commento").click(function(){
var dato= $("#content").val();
var dato2= $("#user_id").val();
var dato3= $("#article_id").val();
var dato4= $("#product_id").val();
var route = "http://localhost:8000/comment";
var token = $("#token").val();
$.ajax({
url: route,
headers:{'X-CSRF-TOKEN':token},
type: 'POST',
dataType: 'json',
data:{
content: dato,
user_id: dato2,
article_id: dato3,
product_id: dato4
},
});
});
タイプを変更しようとするとテキストエリアから隠されたコンテンツの「内容」を確認し、何が起こるかを確認します。コントローラに渡すかどうか –
もし私が隠しに変えれば、私のテキストはコミッションを書くことができます。はい、私のコントローラにコメントテキストが渡されますが、user_idのように隠された他の入力は渡されませんでした。あなたがエラーで見られるかもしれないが、おそらくcomment.jsの問題 –
警告するとどうなるのですか(dato2);クリックイベントで? – Frisbetarian