I am attempting to write some accessor methods according to my best interpretation of the documentation and they do not seem to be working. I am attempting to decrypt an attribute that I am encrypting when it comes into the database via an API call that fires in a scheduled artisan console command. I have a Model that looks like this:
<?php namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use ET_Client;
use ET_DataExtension;
use ET_DataExtension_Row;
use Crypt;
//child implementation
class MasterPreference extends Model {
////these fields can be mass assigned
protected $fillable = [
//
'SMS_OPT_IN',
'EMAIL_OPT_IN',
'CUSTOMER_NAME',
'CUSTOMER_ID'
];
protected $DE_Names = ['MasterPreferences', 'SubPreferences'];
protected $results = '';
//instantiate and replicate
function __construct()
{
}
/**
*
*
* @return $getResult
*/
public function getData()
{
}
/**
* store to the Db
*/
public function store($results)
{
}
/**
* decrypt CUSTOMER_ID
* @param $value
* @return mixed
*/
public function getCustomerIdAttribute($value)
{
return Crypt::decrypt($value);
}
public function getAccountHolderAttribute($value)
{
return $value . 'testing';
}
/**
* ecnrypt Customer ID
*
* @param $value
*/
public function setCustomerIdAttribute($value)
{
$this->attributes['CUSTOMER_ID'] = Crypt::encrypt($value);
}
}
As you can see above I've created 2 accessor methods one for an attribute named CUSTOMER_ID, and another for an attrib named ACCOUNT_HOLDER. When I store like $all = MasterPreference::all() and dd($all) in my index method in the MasterPreferencesController, these attributes are unchanged. Is there another step to calling these accessor methods? Shouldn't they just work by the magic of Laravel?
I appreciate any help! I'm fairly stumped and cannot find this in the docs.
My guess is that Laravel assumes your field names are lower case. If your field name was for example custome_id
it would correctly change the value of it.
You could also try the following:
public function getCustomerIdAttribute($)
{
return Crypt::decrypt($this->attributes['CUSTOMER_ID']);
}
or
public function getCustomerIdAttribute($)
{
return Crypt::decrypt($this->CUSTOMER_ID);
}
then access that value with
MasterPreference::find($id)->customer_id
not sure if and which will work.
Let's see where the magic went wrong.
The Basics:
1) You are extending the class Illuminate\Database\Eloquent\Model
. Functions that you declare in MasterPreference
class, will override the parent class's function, where the names match.
Source: http://php.net/manual/en/keyword.extends.php
The Problem:
MasterPreference
class has an empty __construct
function.
This overrides the __construct()
function of Illuminate\Database\Eloquent\Model
which is:
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = array())
{
$this->bootIfNotBooted();
$this->syncOriginal();
$this->fill($attributes);
}
Your magic of Accessors & Mutators happens in this vortex of code.
The Solution:
Hence, MasterPreference
's __construct
should be as follows:
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
}