php - Laravel Database many:one relation -
i'm learning laravel , building small blog application practice.
i have 'posts' table following structure:
id:int ai post:text author:int
and comments table following structure:
id:int ai comment:text author:int post:int
where post field in comments table meant link comment post.
now, in order add comment, add form in show.blade.php
file in views/posts directory , point store@commentscontroller
metod. hidden field of view automatically populated post's id, store method in comments controlelr like:
post::find(id)->comments->save($comment);
(i'd have defined relations between tables in model fiels, , $comment $comment = comment::create($comment_data_from_form)
)
that , well, however, wondering if laravel had nicer way of passing id of post post view comments controller having specify , populate hidden field?
you use route post id parameter.
to create new comment post use route following.
http://example.org/posts/{id}/comments
and in controller
public function store($postid) { $post = post::findorfail($postid); ... $post->comments->save($comment); }
Comments
Post a Comment