【问题标题】:Laravel 5 Model Accessor Methods not workingLaravel 5 模型访问器方法不起作用
【发布时间】:2015-05-12 01:05:51
【问题描述】:

我正在尝试根据我对文档的最佳解释编写一些访问器方法,但它们似乎不起作用。我正在尝试通过在计划的工匠控制台命令中触发的 API 调用来解密我正在加密的属性。我有一个看起来像这样的模型:

 <?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);
    }



}

正如您在上面看到的,我创建了 2 个访问器方法,一个用于名为 CUSTOMER_ID 的属性,另一个用于名为 ACCOUNT_HOLDER 的属性。当我在 MasterPreferencesController 的索引方法中存储 $all = MasterPreference::all() 和 dd($all) 时,这些属性没有改变。调用这些访问器方法还有其他步骤吗?他们不应该只靠 Laravel 的魔力工作吗?

感谢您的帮助!我很困惑,在文档中找不到这个。

【问题讨论】:

    标签: php laravel eloquent laravel-5 accessor


    【解决方案1】:

    我的猜测是 Laravel 假设您的字段名称是小写的。例如,如果您的字段名称是 custome_id,它将正确更改它的值。

    您还可以尝试以下方法:

    public function getCustomerIdAttribute($)
    {
        return Crypt::decrypt($this->attributes['CUSTOMER_ID']);
    }
    

    public function getCustomerIdAttribute($)
    {
        return Crypt::decrypt($this->CUSTOMER_ID);
    }
    

    然后使用

    访问该值
    MasterPreference::find($id)->customer_id
    

    不确定是否可行,哪个可行。

    【讨论】:

    • 哪一个是正确的?为什么这被标记为正确答案?
    【解决方案2】:

    让我们看看魔法哪里出了问题。

    基础知识:

    1) 您正在扩展类Illuminate\Database\Eloquent\Model。您在MasterPreference 类中声明的函数将覆盖名称匹配的父类的函数。

    来源:http://php.net/manual/en/keyword.extends.php

    问题:

    MasterPreference 类有一个空的__construct 函数。

    这会覆盖Illuminate\Database\Eloquent\Model__construct() 函数,即:

    /**
     * Create a new Eloquent model instance.
     *
     * @param  array  $attributes
     * @return void
     */
    public function __construct(array $attributes = array())
    {
        $this->bootIfNotBooted();
    
        $this->syncOriginal();
    
        $this->fill($attributes);
    }
    

    Accessors & Mutators 的魔力发生在这个代码漩涡中。

    解决方案:

    因此,MasterPreference__construct 应如下所示:

    public function __construct(array $attributes = array())
    {
         parent::__construct($attributes);
    }
    

    【讨论】:

    • 你们在某种程度上都是对的。我昨晚意识到,如果您直接分配属性而不是从模型中提取所有内容,那么魔法确实会发生。喜欢://look them up $all = MasterPreference::all(); $idArray = array(); //values get decrypted by accessor foreach($all as $k =&gt; $val) { $idArray[] = $val-&gt;customer_id; } 但空白构造函数也是一个问题。
    • 是的,他们被故意称为accessors。您必须访问它们才能使魔术发生。即使您调用toArray()toJson(),访问函数也会被应用。
    • 谢谢!你们俩都非常有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多