jeudi 12 octobre 2017

Insert data failed on Laravel one to many relation

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//comment">
        
        <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>
    </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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire