laravel 通过模型关联进行create 操作

 

comment 评论通过模型关联添加  post_id  添加到写入的关联模型中

  

        
        public function comment(Post $post)
        {
            $this->validate();
            
            $comment = new Comment();
            $comment->user_id = \Auth::id();
            $comment->content = request('content');
            $post->comments()->save($comment);
                //模型关联:
                        http://d.laravel-china.org/docs/5.4/eloquent-relationships#写入关联模型
            return back();
        
        }

 

posted @ 2017-08-22 12:36  silvercell  阅读(1707)  评论(0)    收藏  举报