欢迎各位兄弟 发布技术文章
这里的技术是共享的
$user = User::find(10);$profile = new UserProfile();$profile->name = 'Steven';$user->UserProfile()->save($profile);
$user->UserProfile()->name = 'Steven';$user->save();
class User extends Eloquent {
public function profile()
{
return $this->hasOne('Profile'); //Profile is your profile model
}
}
$profile = $user->profile();
$profile->whatever = 'anything';
$user = User::find(10);$profile = $user->UserProfile();$profile->name = 'Steven';$profile->save();
$user = User::find(10);
$profile = $user->profile();
$profile->name = 'Steven';
$user->profile()->save($profile);
$profile = $user->profile();
$profile = $user->profile; // note the removal of ()
$profile = $user->profile()->first();
My Code:$user = User::find(10);$profile = $user->UserProfile;$profile->name = 'Steven';
$user->UserProfile()->save($profile);
look at this posthttp://laravel.io/forum/02-19-2014-relationships-one-to-one?page=1#reply-1691
$user->UserProfile;
'$user->UserProfile;'
$user = User::find(10);
$profile = $user->UserProfile ?: new UserProfile;
$profile->name = 'Steven';
$user->UserProfile()->save($profile);
class User extends Eloquent implements UserInterface, RemindableInterface {
public function profile() {
return $this->hasOne('Profile');
}
...
}
class Profile extends Eloquent {
public function user() {
return $this->belongsTo('User');
}
}
$user = User::find(10);
$profile = $user->profile; // as zenry pointed out the typo.
$profile->name = 'Steven';
$user->profile()->save($profile);
来自 https://laravel.io/forum/02-19-2014-relationships-one-to-one