ドナータイプのユーザーの追加データを含むユーザーモデルとドナーモデルがあります。ユーザーがプロフィールを更新したいときに同時に2つのテーブルを更新したいと思います。以下のコードは次のとおりです。2つのモデルを同時に更新する(Laravel 4.2)
//ユーザーモデル
public function donor() {
return $this->hasone('Donor','u_id');
}
//ドナーモデル
public function user(){
return $this->belongsTo('User','id');
}
//コントローラ
public function updateProfile() {
$input = Input::all();
$lat = Input::get('latitude');
$lng = Input::get('longitude');
$rules = array('username' => 'required|unique:users');
$v = Validator::make($input, $rules);
$id = Auth::user()->id;
if ($v->fails()) {
return Redirect::to('donor/')->withErrors($v)->withInput(Input::all());
} else {
$user = User::find($id);
$donor = User::find($id)->donor;
$user->username = Input::get('username');
$user->name = Input::get('name');
$donor->name = Input::get('name');
$donor->ic = Input::get('ic');
$donor->gender = Input::get('gender');
$donor->blood_type = Input::get('blood_type');
$donor->race = Input::get('race');
$donor->religion = Input::get('religion');
$donor->state = Input::get('state');
$donor->address = Input::get('address');
$donor->lng = $lng;
$donor->lat = $lat;
$donor->email = Input::get('email');
$donor->phone = Input::get('phone');
$user->save();
$donor->save();
return View::make('donorProfile', array('message' => 'Profile successfully updated!'));
}
}
私はドキュメントを追跡しようとしましたが、これまでにそこに行く。ユーザーデータは、ドナーデータではなくbtで保存されます。
いくつかエラーが発生しますか? laravelログを確認してください。 – TheFallen
私はここでエラーが発生しています。$ donor = User :: find($ id) - >列IDのドナー。 – user94743
ただし、投稿を編集してエラーメッセージを貼り付けることはできますか? – TheFallen