访问器未被调用
Accessor is not called
我有一个名为 Order 的模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Order extends Model
{
public function getsystemIdAttribute()
{
return 99;
}
public function setCreatedAtAttribute($value){
$this->attributes['createdAt'] = Carbon::parse($value)->format('Y-m-d');
}
protected $fillable = ['customerId', 'itemId','systemId', 'createdAt'];
public $timestamps = false;
}
问题是访问器 - getsystemIdAttribute()suprise 没有被调用,我在其他字段上尝试过,但是访问器没有被调用 again.Of 当然我已经从 getsystemIdAttribute 更改了名称getitemIdAttribute(例如)。我把这个方法复制到另一个模型上,再次稍微更改了名称,它起作用了。我启动了我的调试器,并访问了使用该模型的视图,令我惊讶的是该方法没有被调用,但是数据库中的字段被返回了。 Laravel的版本是5.3,数据库是MySQL,php的版本是7。
知道我的问题是什么吗。
编辑:
这是驼峰式大小写的代码:
public function getItemIdAttribute($value)
{
return 99;
}
public function getSystemIdAttribute($value)
{
return 99;
}
但它再次不起作用,它从 database.Here 返回数据是从数据库检索数据的方法:
public function read_many($count, $page, $table){
$tbl = Models::determine_table($table);
return $tbl::skip(intval($count)*(intval($page)-1))->take(intval($count))->get()->toArray();
}
这里是 determine_table 方法:
public static $tables = [ 'systems' => 'App\System',
'orders' => 'App\Order',
'notifiers' => 'App\Notifier',
'billings' => 'App\Billing',
'items' => 'App\Item',
'customers' => 'App\Customer',
'users' => 'App\User'
];
public static function determine_table($table_input){
foreach(self::$tables as $table=>$class){
if($table_input == $table){
return $class;
}
}
die();
}
您需要将方法写在studly案例中(如here所述)!
所以会是 getSystemIdAttribute()
或 getItemIdAttribute()
当你现在执行 dd(Order::find(1)->system_id);
它应该 return 99 以防你在数据库中有一个 Order 对象。
编辑: 当使用 toArray()
时,修改器将不会被执行!查看 this comment 以获得一个很好的解释为什么它是这样的。
我有一个名为 Order 的模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Order extends Model
{
public function getsystemIdAttribute()
{
return 99;
}
public function setCreatedAtAttribute($value){
$this->attributes['createdAt'] = Carbon::parse($value)->format('Y-m-d');
}
protected $fillable = ['customerId', 'itemId','systemId', 'createdAt'];
public $timestamps = false;
}
问题是访问器 - getsystemIdAttribute()suprise 没有被调用,我在其他字段上尝试过,但是访问器没有被调用 again.Of 当然我已经从 getsystemIdAttribute 更改了名称getitemIdAttribute(例如)。我把这个方法复制到另一个模型上,再次稍微更改了名称,它起作用了。我启动了我的调试器,并访问了使用该模型的视图,令我惊讶的是该方法没有被调用,但是数据库中的字段被返回了。 Laravel的版本是5.3,数据库是MySQL,php的版本是7。
知道我的问题是什么吗。
编辑:
这是驼峰式大小写的代码:
public function getItemIdAttribute($value)
{
return 99;
}
public function getSystemIdAttribute($value)
{
return 99;
}
但它再次不起作用,它从 database.Here 返回数据是从数据库检索数据的方法:
public function read_many($count, $page, $table){
$tbl = Models::determine_table($table);
return $tbl::skip(intval($count)*(intval($page)-1))->take(intval($count))->get()->toArray();
}
这里是 determine_table 方法:
public static $tables = [ 'systems' => 'App\System',
'orders' => 'App\Order',
'notifiers' => 'App\Notifier',
'billings' => 'App\Billing',
'items' => 'App\Item',
'customers' => 'App\Customer',
'users' => 'App\User'
];
public static function determine_table($table_input){
foreach(self::$tables as $table=>$class){
if($table_input == $table){
return $class;
}
}
die();
}
您需要将方法写在studly案例中(如here所述)!
所以会是 getSystemIdAttribute()
或 getItemIdAttribute()
当你现在执行 dd(Order::find(1)->system_id);
它应该 return 99 以防你在数据库中有一个 Order 对象。
编辑: 当使用 toArray()
时,修改器将不会被执行!查看 this comment 以获得一个很好的解释为什么它是这样的。