This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
program.es
I want to set number format for food_json[price] and drink_json[price]. If fields defined as a following jsonable. How to define an accessor from doc https://octobercms.com/docs/database/mutators#accessors-and-mutators.
food_json[name]:
label: Name
type: text
food_json[option]:
label: Option
type: text
food_json[price]:
label: Price
type: number
drink_json[name]:
label: Name
type: text
drink_json[option]:
label: Option
type: text
drink_json[price]:
label: Price
type: number
I try following but It doesn't work.
public function getFoodJsonPriceAttribute($value)
{
return number_format($value);
}
public function getDrinkJsonPriceAttribute($value)
{
return number_format($value);
}
I also try following but It doesn't work too.
public function getFoodJsonAttribute($value)
{
return number_format($value->price);
}
public function getDrinkJsonAttribute($value)
{
return number_format($value->price);
}
program.es
Solved with json_decode and json_encode.
public function getFoodJsonAttribute($value)
{
$value = json_decode($value);
$value->price = number_format($value->price);
return json_encode($value);
}
public function getDrinkJsonAttribute($value)
{
$value = json_decode($value);
$value->price = number_format($value->price);
return json_encode($value);
}
1-2 of 2