I am want to update my records in my database using Eloquent model . I am having an error in updating user_profile
table that saying where id is not null
.
User Model
class User extends Model
{
public function profile()
{
return $this->hasOne('App\Models\Common\UserProfile');
}
}
UserProfile Model
class UserProfile extends Model
{
public function user()
{
return $this->belongsTo('\App\Models\User');
}
}
Controller
$user->fill($data);
$user->save();
$user->profile->fill($data);
$user->profile->save();
In MySQL query it looks like this:
UPDATE users, user_profiles
SET users.name='Test Name',
user_profiles.email='test@gmail.com'
WHERE users.id=1
AND user_profiles.user_id=1;
I know MySQL query but I'm new to Eloquent
and I want to do it in Eloquent model
. Maybe there are some in here that can provide an explanation on how the magic works in Eloquent
.
By default eloquent assumes, that any model has an id
column as primary key. If that is not the case, you need to "register" the primary key with
protected $primaryKey = 'user_id';
in the model class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.