2017-05-16 16 views
0

ユーザーモデル:Laravelの更新に関連するテーブル

class User extends Authenticatable { 
    use Notifiable; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'name', 'email', 'password', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

    public function userInformation() { 
     return $this->hasOne('App\UserInformation', 'user_id', 'id'); 
    } 
} 

User_Informationモデル:私はその関数のユーザーテーブルの列を更新することができ

public function updateUser($request, $id) { 
     $user = User::findOrFail($id); 
     if (!empty($request->input('email'))) $user->email = $request->input('email'); 
     if (!empty($request->input('password'))) $user->password = $request->input('password'); 
     if (!empty($request->input('type')) || $request->input('type') === '0') $user->type = $request->input('type'); 
     if (!empty($request->input('package_size')) || $request->input('package_size') === '0') $user->package_size = $request->input('package_size'); 
     if (!empty($request->input('package_expiration_date'))) $user->package_expiration_date = $request->input('package_expiration_date'); 
     if (!empty($request->input('is_deleted')) || $request->input('is_deleted')) $user->is_deleted = $request->input('is_deleted'); 

     if (!empty($request->input('title'))) $user->userInformation()->title = $request->input('title'); 
     if (!empty($request->input('first_name'))) $user->userInformation()->name = $request->input('first_name'); 

     $user->save(); 

     return $user; 
    } 

私:ここ

class UserInformation extends Model { 

    /** 
    * The table associated with the model. 
    * 
    * @var string 
    */ 
    protected $table = "user_information"; 

    public function users() { 
     return $this->belongsTo('App\User', 'user_id', 'id'); 
    } 
} 

は、更新部分ですuser_information列にアクセスして更新することはできません。

どうすれば更新できますか?

ユーザーテーブルのID = user_informationテーブルのUSER_ID ....

P.S.私はEloquent働く原則を知らない、私はDoctrineと働いていた。すべての入力があなたと同じ名前を持っているのでhttps://laravel.com/docs/5.3/eloquent-relationships#one-to-one

この

$user->userInformation()->save($userInformation); 

答えて

1

などのユーザー情報を保存することを忘れないでください:

+0

パッチを適用したい場合はどうすればよいですか?それは仕事ですか?私のコードでは、私は空のパラメータに送信する場合、私は更新されません。 – Nevermore

+0

上記のコードは更新/パッチ適用のためのコードです。いくつかのフィールドまたはすべてのフィールドの更新と同じです。 'email'と' password'だけを指定すると、それらのフィールドだけが更新されます。あなたはコードをテストして自分自身で見ることができます。 – Sandeesh

+0

はい、空の入力を送信すると、更新からスキップされます。 – Sandeesh

0

この

$userInformation = $user->userInformation; 
$userInformation->title = $request->input('title'); 

ソースをしようデータベースフィールドでは、一括割り当てを使用することができます。ただし、モデルにはfillableプロパティを設定してください。または、必要な入力と更新のみを抽出することもできます。以下のコードは、データベースフィールドに対応する入力が提供された場合にのみ更新され、そうでない場合は無視されます。

//User 
protected $fillable = [ 
    'name', 'email', 'password', 'type', 'package_size', 'package_expiration_date', 'is_deleted' 
]; 

//UserInformation 
protected $fillable = [ 
    'title', 'first_name' 
]; 

$user->update(array_filter($request->all())); 

$user->userInformation()->update(array_filter($request->only(['title','first_name']))); 

// OR 

$user->update(array_filter($request->only([ 
    'email', 
    'password', 
    'type', 
    'package_size', 
    'package_expiration_date', 
    'is_deleted', 
]))); 

$user->userInformation()->update(array_filter($request->only([ 
    'title', 
    'first_name', 
]))); 
0

UserInformationを別々に入力できます。

$user = User::findOrFail($id); 
$user->email = $request->input('email'); 
... 
if($user->save()) 
{ 
    $userInfo = App\UserInformation::where('user_id',$id); 
    $userInfo->title = $request->input('title'); 
    $userInfo->name = $request->input('first_name'); 
    $userInfo->save(); 
} 

または、associate()メソッドを使用して、belongsTo関係を更新することができます。

$userInfo->user()->associate($user); 
$userInfo->save(); 
関連する問題