laravel怎樣關聯插入資料?
phpcn_u1582
phpcn_u1582 2017-05-16 16:55:17
0
1
901

懸而未決:

表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學習者快速成長!