Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SigalZ's avatar

Accessor int to float

Hello,

I have an int field in a table, called: weight that stores weight in grams.

I don't want to store it as decimal in the table.

When displaying it, I want to display it as kg.

e.g. if the value is 250 display: 0.25, if it is 2500 display: 25.

I tried to use accessor in my model:

public function weight(): Attribute
{
        return new Attribute(
            get: fn($value) => $value / 1000,
           //I also tried:
           get: fn($value) => number_format($value / 1000, 2, ','),
           //I also tried:
           get: fn($value) => floatval($value / 1000),
        );
    }

All of these tries display the field as is, e.g. if the value is 250 it shows me 250 and not .25

I added this as well:

protected $casts = [
        'weight' => 'float',
    ];

Still displays 250

I tried to display it with:

number_format($value, 2, ',')

it displays 250,00

Is there a way to do what I want?

0 likes
12 replies
Snapey's avatar

Seems like your accessor is not being called at all?

sr57's avatar

When displaying it,

How do you get the value (accessor works only with eloquent)?

What's the result with tinker?

SigalZ's avatar

@sr57 I am calling it in a Livewire component:

the route for the component: Route::get('/bags/{bag}/edit' , App\Http\Livewire\Admin\Bag\BagForm::class)->name('bags.edit');

The component's code:

use Livewire\Component;
use Redirect;
use App\Models\Bag;

class BagForm extends Component
{
    protected array $rules = [];

    public $weight;
    public $colour;
    public $stock = 0;
    public $lowStockWarn = 0;
    
    public Bag $bag;

    public function mount(Bag $bag)
    {
        $this->bag = $bag;
        dd($bag->weight);
                
        if(!is_null($this->bag->id)) {
            $this->fill([
                'weight' => number_format($bag->weight, 2, ','),
                'colour' => $bag->colour,
                'stock' => $bag->stock,
                'lowStockWarn' => $bag->low_stock_warn,
            ]);
        }
    }
...

I never used tinker before, so I'm writing what I did just to make sure that's what you meant:

php artisan tinker
$bag = App\Models\Bag::find(1)

App\Models\Bag {#4858
     id: 1,
     weight: 250,
     colour: "Black",
     stock: 657,
     low_stock_warn: 100,
     deleted_at: null,
     created_at: "2022-08-01 18:45:41",
     updated_at: "2022-08-05 14:29:22",
   }

Thank you

Snapey's avatar

@SigalZ in tinker, after what you have done already, $bag->weight

also, put dd() in your accessor

SigalZ's avatar

@Snapey $bag->weight shows 250.0 Where should I put the dd() in the accessor?

Snapey's avatar

@SigalZ Actually, your accessor looks wrong?

it should be calling a make function?

public function weight(): Attribute
{
	return Attribute::make(
            get: fn($value) => $value / 1000,
        );
    }

perhaps yours is an alternate syntax?

Snapey's avatar

@SigalZ to add dd()

public function weight(): Attribute
{
	return Attribute::make(
            get: fn($value) => dd($value)
        );
    }
SigalZ's avatar

@Snapey I was following Jeffery Way's tutorial here: https://laracasts.com/series/whats-new-in-laravel-9/episodes/11

That's how he uses it.

I tried your way as well, didn't change anything.

It's like it is not looking at the model.

Very strange thing, I change a different model, called: Box like this:

public function maxWeight(): Attribute
    {
        return new Attribute(
            //get: fn($value) => floatval($value / 1000),
            get: fn($value) => 'test',
            set: fn($value) => $value * 1000
        );
    }

I have a box with max_weight = 2000 in the database, fetching it in tinker, returns the value divided by 1000...

What the....

frankielee's avatar
Level 29

Make sure you have imported the correct Class

use Illuminate\Database\Eloquent\Casts\Attribute;
2 likes
SigalZ's avatar

@frankielee This wansn't even in the model...

I added it, but it didn't change anything.

This is all very strange.

Maybe I should do clear:cache or something?

SigalZ's avatar

HA! I found something.

I tried it all in Visual Code Terminal.

Trying it in cmd it shows it correctly.

Also adding the use statement helped. Restarting VS code, did the job.

Thank you all!!!!;

Please or to participate in this conversation.