php - attribute doesn't add in stdClass object -
i developing web application in laravel 5.3. problem getting attributes doesn't add in stdclass object, can see code explicitly setting/adding new attributes in object.
problematic code
1- $sender = sender::where('id', $this->senderid)->first(); 2- if ($sender != null) { 3- var_dump($sender->bundle); 4- $sender->bundle->latitude = $this->lat; 5- $sender->bundle->longitude = $this->long; 6- $sender->bundle->location = $response->formattedaddress(); 7- var_dump($sender->bundle); 8- $sender->save(); 9- error_log('resolved'); 10- } else { 11- error_log('no sender'); 12- } in above code can see setting new properties bundle object.
$sender->bundle->latitude = $this->lat; $sender->bundle->longitude = $this->long; $sender->bundle->location = $response->formattedaddress(); but in output can see doesn't add up. code runs in laravel queue. in above code $sender laravel eloquent model , attribute bundle being json_decode via laravel mutators.
mutator code
public function setbundleattribute($value) { $this->attributes['bundle'] = json_encode($value); } public function getbundleattribute($value) { return json_decode($value); } output of above problematic code
object(stdclass)#686 (3) { #output of line 3 ["city"]=> string(0) "" ["province"]=> string(0) "" ["country"]=> string(0) "" } object(stdclass)#686 (3) { #output of line 7 ["city"]=> string(0) "" ["province"]=> string(0) "" ["country"]=> string(0) "" } [2017-07-25 16:11:03] processed: app\jobs\locationresolverqueue resolved problem solved
laravel accessors & mutators cause problem. accessor , mutator called whenever or set property respectively, in case mutator calling whenever try access $sender->bundle->property accessor didn't called because setting $sender->bundle->property rather $sender->bundle solved problem like
$sender = sender::where('id', $this->senderid)->first(); if ($sender != null) { $temp = $sender->bundle; $temp->latitude = $this->lat; $temp->longitude = $this->long; $temp->location = $response->formattedaddress(); $sender->bundle = $temp; $sender->save(); }
Comments
Post a Comment