http://laravelacademy.org/post/7014.html
我系统中存储单位是分,用户界面展示的是元。 应用场景:商品、用户余额
现在: 添加新数据( insert )、获取数据( select )没毛病,能自动的 再 insert 的时候 乘 100,select 出来的时候 除 100
但是问题来了,这个东西竟然不支持 在更新数据的时候( update )自动 乘 100
使用 自增 increment 和 自减 decrement 不会自动 乘 100
坛子里有小伙伴们碰到吗
举例:
/**
* 商品
* Class Goods
*
* @package App\Http\Models
*/
class Goods extends Model
{
protected $table = 'goods';
protected $primaryKey = 'id';
function getPriceAttribute($value)
{
return $value / 100;
}
function setPriceAttribute($value)
{
$this->attributes['price'] = $value * 100;
}
}
$amount = 10;
Goods::query()->increment('price', $amount);
希望结果:goods 的 price 加 1000
实际结果:goods 的 price 只加了 10