doukui2011 2015-05-12 01:05
浏览 616
已采纳

Laravel 5模型访问器方法不起作用

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.

  • 写回答

2条回答 默认 最新

  • dqy92287 2015-05-12 01:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决