i get this error when i want add comment on my posts after relationship
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'Posts_ID' cannot be null (SQL: insert into
Comment
(Comment
,posts_id
,updated_at
,created_at
) values (dwa, , 2018-02-03 16:47:58, 2018-02-03 16:47:58))
Route :
Route::get('/showpost/{showpost}/create/comment', function($ID) {
$showpost = Posts::find($ID);
return view('createcomment', compact('showpost'));
});
Route::post('/showpost/{showpost}/create/comment', 'PostController@storecm');
Controller :
public function storecm(Posts $showpost, Request $request)
{
$showpost->Comment()->create($request->all());
}
View :
<form action="{{ action('PostController@storecm', $showpost->ID) }}" method="post">
{!! csrf_field() !!}
<label>Comment :</label>
<input type="text" name="Comment" id="Comment">
<button type="submit" class="btn btn-success">Add Comment</button>
</form>
comment()
relationship set up in yourPosts
model? 2) If you don't, add one. If you do, then you probably need to specify the foreign key asposts_id
(plural) since by default it'll be assumingpost_id
(singular). Also, you may want to look at MVC naming conventions. For example, model class names are typically singular (they represent an instance of the thing). If you follow these, then your relationships will be more likely to work without modifications. (laravel.com/docs/5.5/eloquent#eloquent-model-conventions) – tptcat