Reputation: 1743
User Model :
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 Model :
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');
}
}
Here is update part :
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;
}
I can update users table columns in that function but i can not access and update any user_information column.
How can i update ?
users table's id = user_information table's user_id....
p.s. I dont know Eloquent working principles, i was working with Doctrine.
Upvotes: 0
Views: 1097
Reputation: 11906
Since all your inputs have the same name as your database fields, you can use mass assignment. But be sure to set the fillable
property in the model. Or you could extract only the required inputs and update. The below codes will only update when the inputs corresponding to the database fields are provided, if not they would be ignored.
//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',
])));
Upvotes: 1
Reputation: 3266
You can populate UserInformation
separately.
$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();
}
Or, you may use associate() method to update belongsTo relationship.
$userInfo->user()->associate($user);
$userInfo->save();
Upvotes: 0
Reputation: 2523
Try to do this
$userInformation = $user->userInformation;
$userInformation->title = $request->input('title');
Source : https://laravel.com/docs/5.3/eloquent-relationships#one-to-one
Don't forget to save the user information like this
$user->userInformation()->save($userInformation);
Upvotes: 0