首页 > laravel怎样关联插入数据?

laravel怎样关联插入数据?

悬而未决:

表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 了……


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

【热门文章】
【热门文章】