There was a problem. Previously, the share_price
column was a float
, after which we had to change it to a string
, because the client requested the use of not only floats, but also words.
To display the minimum and maximum values in the filter, use the min
and max
function.
But since appeared string, max value is now always string.
How to convert column to float in max
wrapper?
$companyList = $companyList->get();
$maxValue = $companyList->max('share_price');
UPDATE
It seems you misunderstood me a bit. This does not mean the conversion of the already obtained value, but when selecting the max value.
Example:
- 1.52
- 2.43
- 3.35
- STRING
When i get ->max()
– i get STRING, but need to get 3.35
4
Answers
You can try
floatval
function of PHP.or you can also cast your string value into float like below:
Cast $maxValue as int or float
or the hacky way
Since you’re already using a collection, I’d suggest using filter to filter out all non-numeric items.
The
IlluminateSupportTraitsEnumeratesValues::max
method accepts only one argument that can be either:string
that acts as the key to check in the collectioncallable
(a function) that you may use to filter outstring
s in your case.In your case, you can use a callback function that removes the
string
s, or precisely, treats thestring
s asnull
.The above code will:
null
if the collection is empty or contains only strings.