Laravel decimals are uploaded as strings

When I upload my products, using php artisan scout:import “modelx”, prices are uploaded as strings which causes problems to the price filtering.

Is there any way I can enforce a specific type for the price attribute?

In my database, the price column is stored as DECIMAL(10,2)

Hi Chrys,

You can force the type by overriding the toSearchableArray() method in your model. For instance:

public function toSearchableArray()
{
    $record = $this->toArray();

    $record['price'] = (float) $this->price;

    return $record;
}

That’s actually what I was doing and wasn’t working earlier :confused: I think I forgot to deploy the changes :smiley:

Btw, just in case someone else sees it, instead of $this->record() the correct method is $this->toArray().

Thanks julien

Oops! Thank you. I’ll update my post :slight_smile: