alexalejandroem
alexalejandroem

Reputation: 1162

Laravel treating decimal as string

Im trying to make the next eloquent query

$result = $result->where('money','>=',(float)$moneyFilter);

The money column in my database its DECIMAL(11,2), when I run the query it returns an empty array, when I go over php artisan tinker and see the column money, it's a string value "11.1".

I would like to filter the $result collection to have values over $moneyFilter.

Thanks

Upvotes: 5

Views: 5449

Answers (1)

Mahdi Younesi
Mahdi Younesi

Reputation: 7509

You need to define in your model which fields need to be cast to a primitive attribute.

  protected $casts = ['my_decimal' => 'float'];

There is a really good explanation here:

https://mattstauffer.com/blog/laravel-5.0-eloquent-attribute-casting/

Also there is an explanation in the docs:

https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting

Upvotes: 2

Related Questions