Reputation: 1422
I try to insert some data in one to many relation and i got this kind of error:
SQLSTATE[HY000]: General error: 1364 Field 'article_id' doesn't have a default value (SQL: insert into `comments` (`comment`, `updated_at`, `created_at`) values (this is comment, 2017-10-12 13:14:07, 2017-10-12 13:14:07))
here's my router:
Route::post('/article/{article}/comment', 'CommentsController@save');
here's my controller:
public function save(Article $article){
Comment::create([
'comment' => request('comment'),
'article_id' => $article->id
]);
return back();
}
And here is my form that i use to post the data:
<form method="post" class="form-horizontal" action="/article/{{ $article->id }}/comment">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="comment" name="comment"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Simpan</button>
</div>
</div>
</form>
I try to print my $article->id and it has a value of my article id. but i don't know why it said that it doesn't have value. Thanks.
Upvotes: 1
Views: 56
Reputation: 21681
You should try this:
Please add below code in Comment
model and check:
protected $fillable = ['comment','article_id'];
Upvotes: 1