laravel怎样关联插入数据?
phpcn_u1582
phpcn_u1582 2017-05-16 16:55:17
0
1
891

悬而未决:

表Users
表UserProfiles

Users对UserProfiles是hasOne关系

  public function hasOneProfile(){

    return $this->hasOne('App\UserProfile','user_id','id');

  }

UserProfiles对Users是belongsTo关系

  public function belongsToUser(){

    return $this->belongsTo('User','user_id','id');

  }

当一个新的 $user = new Users(); 被创建时

如何直接写 $user 的属性,可以达到 save() 的时候可以把 UserProfile 一并直接创建呢?

比如说:
只给 UserProfile 指定一个属性 sex=1 然后怎么能直接当成新的 User 和 UserProfile 一并 Create 了……

phpcn_u1582
phpcn_u1582

全部回复(1)
洪涛

在save之前就创建user profile.userprofile创建成功才能save。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!