Laravel 5 模型访问器方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30180068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 Model Accessor Methods not working
提问by funkenstein
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:
我试图根据我对文档的最佳解释编写一些访问器方法,但它们似乎不起作用。我试图解密我正在加密的属性,当它通过在预定的 artisan 控制台命令中触发的 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);
}
}
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?
正如您在上面看到的,我创建了 2 个访问器方法,一个用于名为 CUSTOMER_ID 的属性,另一个用于名为 ACCOUNT_HOLDER 的属性。当我在 MasterPreferencesController 的索引方法中存储类似 $all = MasterPreference::all() 和 dd($all) 时,这些属性保持不变。调用这些访问器方法还有其他步骤吗?他们不应该只是通过 Laravel 的魔法工作吗?
I appreciate any help! I'm fairly stumped and cannot find this in the docs.
我感谢任何帮助!我很难过,在文档中找不到这个。
回答by Pawel Bieszczad
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.
我的猜测是 Laravel 假设您的字段名称是小写的。例如custome_id
,如果您的字段名称是,它将正确更改它的值。
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.
不确定是否以及哪个会起作用。
回答by Mysteryos
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.
1)您正在扩展课程Illuminate\Database\Eloquent\Model
。您在MasterPreference
类中声明的函数将覆盖名称匹配的父类的函数。
Source: http://php.net/manual/en/keyword.extends.php
来源:http: //php.net/manual/en/keyword.extends.php
The Problem:
问题:
MasterPreference
class has an empty __construct
function.
MasterPreference
类有一个空__construct
函数。
This overrides the __construct()
function of Illuminate\Database\Eloquent\Model
which is:
这覆盖了其__construct()
功能Illuminate\Database\Eloquent\Model
是:
/**
* 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:
因此,MasterPreference
's__construct
应如下所示:
public function __construct(array $attributes = array())
{
parent::__construct($attributes);
}