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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记