简体   繁体   中英

Laravel orderBy not returning correct results

My product model has range of products with values like 20.00 or 1000.00 however using Product::orderBy('price_1', 'desc')->take(10)->get(); the result does not show 1000.00 first it shows somewhere closer to the 20.00. Is this because of the . in the value or is there a better way to get the highest price first?

Change the type of the column to DECIMAL(6,2) (or any other precision).

The reason the fields are sorted like that is because it's a VARCHAR and varchars are sorted alphabetically, not by numeric value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM