Environment
- Laravel 9
- php 8.0
I have this mutator function to transform a value from 4 decimal places to 2 decimal places. I want to test out but the Attribute::make
function not returning value, below is code for the model
class SubjectWithFee extends Model
{
protected $table = 'sjfee';
protected $primaryKey = 'IndexID';
protected $fillable = [
'Amount',
'CurrencyID',
'ProgramID',
];
public $timestamps = false;
protected function Amount(): Attribute
{
return Attribute::make(
get: fn ($value) => sprintf('%0.2f', $value),
);
}
Although when I did the test it access the attribute correctly when putting dd('test')
before the return but on the get
function cannot be access
Anyone knows the problem?
Update
The column "Amount" starts with capital letter
Initially the source code was from laravel version 5 and someone upgraded to 9.
Update 2
All my setters are working, but only getters are not.
2
Answers
Try this different approach, instead of the current function you have:
Add the attribute name to the appends property of your model.
Appending Values To JSON