Php Laravel getDirty()双精度和整数
我可以在我的应用程序中创建和记录以下历史记录:Php Laravel getDirty()双精度和整数,php,laravel,laravel-5,Php,Laravel,Laravel 5,我可以在我的应用程序中创建和记录以下历史记录: public static function boot() { parent::boot(); self::updating(function($item) { $changes = array(); foreach ($item->getDirty() as $key => $value) { switch ($key) {
public static function boot()
{
parent::boot();
self::updating(function($item)
{
$changes = array();
foreach ($item->getDirty() as $key => $value) {
switch ($key) {
case 'supplierId':
$supplier1 = Supplier::find($item->original[$key]);
$supplier2 = Supplier::find($value);
$changes[] = 'Changed supplier fee from ' . $supplier1->name . ' to ' . $supplier2->name;
break;
case 'typeId':
$from = EnquiriesBuyingFeesTypes::find($item->original[$key]);
$to = EnquiriesBuyingFeesTypes::find($value);
$changes[] = 'Changed fee type from ' . $from['charge'] . ' to ' . $to['charge'];
break;
case 'currencyId':
$from = Currency::find($item->original[$key])->symbol;
$to = Currency::find($value)->symbol;
$changes[] = 'Changed fee currency from ' . $from . ' to ' . $to;
break;
case 'net':
$changes[] = 'Changed fee net from ' . $item->original[$key] . ' to ' . $value;
break;
default:
$changes[] = 'Changed fee ' . $key . ' from ' . $item->original[$key] . ' to ' . $value;
break;
}
}
foreach ($changes as $change) {
Enquiry\History::create(array(
'enquiryId' => $item->enquiryId,
'details' => $change
));
}
});
}
问题是,当任何字段更改时,它总是认为“net”已更改。示例结果为“将费用净额从3.00更改为3”
如何使getDirty()函数将net的任何整数视为十进制,这样3.00与3.00的比较就不会被记录为更改?尝试新的雄辩强制转换,这可能会解决您的问题
因此,通过执行上述操作,Eloquent会将net属性转换为double,因此您每次请求时都会收到double,即使是3.00谢谢,但没有效果:(
class Model extends Eloquent {
protected $casts = [
'net' => 'double',
]
}