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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效