【发布时间】:2017-03-21 18:43:54
【问题描述】:
我有 3 个表与 hasOne() 链接。模型 User.php 与 Profile.php 链接 Profile.php 与 Country.php 链接,Profile.php 与 City.php 链接。
在 Profiles 表中有 user_id 外键。在 Countrys 表中有 profile_id 外键。并且在 Citys 表中有 profile_id 外键。
我怎样才能正确地用用户 ID 自动填充这个外键。
现在我在 RegistersUsers.php 中这样做
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
$user_id = Auth::user()->id;
$profile = new Profile;
$profile->user_id = $user_id;
$profile->save();
$country = new Country;
$country->profile_id = $user_id;
$country->save();
$city = new City;
$city->profile_id = $user_id;
$city->save();
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
但我认为有一种更正确的方法。
【问题讨论】:
标签: laravel-5