I have in my model accessors and mutators for hash/rehash data in database table fields. For example:
public function setFullNameAttribute($value)
{
$this->attributes['full_name'] = Helper::geted('encrypt', $value);
}
public function getFullNameAttribute($value)
{
return Helper::geted('decrypt', $value);
}
When I'll save data to database all sending datas saving in hashed form but on update data not hashed. My save/update code:
$profile = [
'full_name' => "John",
'address' => "United Kingdom"
];
$profile_save = new Profile($profile);
$exist = Personal::where('user_id', Auth::id())->count();
if($exist == 0) $user->profile()->save($profile_save);
if($exist == 1) $user->profile()->update($profile);
When I first time save this info to db:
When I second time enter to current URL data will be updated:
Why does not the information be stored in an encrypted form when updating information?
Your problem is in this line:
$user->profile()->update($profile);
Mutators and accessors work on eloquent and not on Query Builders. You are using update function which is a query builder function, so you are updating your database directly. Use this:
$profile = [
'full_name' => "John",
'address' => "United Kingdom"
];
$profile = auth()->user()->profile;
if ($profile) {
$profile->full_name = $profile['full_name'];
$profile->address = $profile['address'];
$profile->save();
} else {
auth()->user()->profile()->create($profile);
}
For anybody who has the same problem:
Just omitting the parantheses also does the trick, as this uses Eloquent instead of the Query Builder:
$user->profile->update($profile);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With