2017-05-16 65 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型號:

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'); 
    } 
} 

這裏是更新的一部分:

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; 
    } 

我可以在功能更新用戶表列,但我無法訪問和更新任何user_information列。

我該如何更新?

users表中的id = user_information表的USER_ID ....

附:我不知道雄辯的工作原則,我正在與教義。

回答

1

用戶信息。由於所有的輸入具有相同的名稱數據庫字段,您可以使用批量分配。但一定要在模型中設置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

如果我想補丁怎麼辦?那是工作嗎?對於我的代碼,如果我發送給空參數,我不會更新。 – Nevermore

+0

以上代碼用於更新/修補。這與更新少量字段或所有字段相同。如果你只提供'email'和'password',那麼只有這些字段會被更新。您可以測試代碼並親自查看。 – Sandeesh

+0

是的,如果你發送一個空的輸入,那麼這些將被從更新中跳過。 – Sandeesh

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()方法來更新屬於關聯關係。

$userInfo->user()->associate($user); 
$userInfo->save();