PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 19, 2022

[FIXED] Laravel eloquent mutators not work on update data

 March 19, 2022     eloquent, laravel, laravel-5.6, php     No comments   

Issue

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:

enter image description here

When I second time enter to current URL data will be updated:

enter image description here Why does not the information be stored in an encrypted form when updating information?


Solution

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


Answered By - train_fox
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing