Hallo, ich mache eine CRUD mit Ajax, ich habe ein Problem mit Store Kommentar eingereicht. Ich habe diesen Fehler:Laravel 5.1 - Crud mit 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))
Ich habe einen Kommentar mit dem Text „mein Kommentar“ speichern tryed. Mein Input "content" wird an meinen Controller übergeben, aber meine Eingabe wie "article_id", "user_id", "product_id" wird nicht an meinen Controller weitergegeben.
Comment:
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!"
]);
}
}
Formular Kommentar Artikel:
{!! Form::open(['route'=>'comment.store'])!!}
<div class="form-group">
<label for="reply-text" class="sr-only">Commenta</label>
{!! Form::textarea('content', null, ['id'=>'content','class'=>'form-control','rows'=>'3', 'placeholder'=>'Commenta','required'])!!}
</div>
<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}">
{!! Form::hidden('user_id', Auth::user()->id, null,['id'=>'user_id','class' =>'form-control'])!!}
{!! Form::hidden('article_id', $article->id, null,['id'=>'article_id','class' =>'form-control'])!!}
{!! Form::hidden('article_slug', $article->slug, null,['id'=>'article_slug','class' =>'form-control'])!!}
{!!link_to('#', $title='Comment post', $attributes =['id'=>'commento', 'class'=>'btn btn-lg btn-dark btn-outline'], $secure = null)!!}
{!! Form::close()!!}
comment.js:
$("#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
},
});
});
Können Sie versuchen, den Typ zu ändern? von 'content' von textarea zu versteckt und sehen was passiert? Ob es an deinen Controller weitergegeben wird oder nicht? –
Wenn ich zu versteckt ändere, kann ich sehen, dass mein Text ein Kommment schreiben soll, ja, der Kommentartext wird an meinen Controller übergeben, aber andere Eingaben, die wie user_id ausgeblendet wurden, wurden nicht übergeben. Wie Sie mit Fehler sehen können, vielleicht ein Problem mit comment.js –
Was bekommen Sie, wenn Sie alarmieren (DATO2); im Click-Event? – Frisbetarian