如何在Laravel中从数据库最小到最大获取数据

时间:2018-12-03 06:50:42

标签: laravel sorting laravel-5 laravel-5.2

我有一个像这样的餐桌产品:

enter image description here

我正在从产品表中获取数据,但我需要按价格从低到高的数据。我试图通过升序获得产品价格,但我正在按1,2,3,4升序获得商品现在,我们有80,900,100,30的价格,我需要像这样的30,80,100,900。我已经尝试过该查询,但是没有得到确切的结果。

$low_to_high = DB::table('products')
                   ->where([
                            ['products_categories_id', $id]
                          ])->orderBy('price','ASC')->get();

结果数组:

Array
(
    [0] => stdClass Object
        (
            [id] => 12
            [user_id] => 5
            [products_categories_id] => 22
            [first_cat] => 1
            [sec_cat] => 3
            [price] => 110
            [title] => ARTIST COLOR PENCIL
            [image] => http://banatapp.com/Bannat_app/uploads/product/1543473823extremecleanser_44062_0.png
            [description] => AQUA XL INK LINER is a waterproof liquid eyeliner which hugs lash lines with comfort and instant color intensity for 24-hour lasting.
This athleisure color collection features 11 intense liquid eyeliner shades from matte to lustrous, diamond and metallic, with a luminous finish. This waterproof yet very flexible, light liquid texture will hug all lash lines with comfort and instant color intensity for 24-hour lasting. The semi-flex applicator offers a lining range from high-precision to extra thick coloring. Featuring a color-coded tip and cap, the secure closing system guarantees airtightness and preserves the color’s intensity.
            [sale] => 
            [qty] => 25
            [options] => http://banatapp.com/Bannat_app/uploads/product/1543473823Color_Airbrush_M100.png|http://banatapp.com/Bannat_app/uploads/product/1543473823Color_Ink_M100.png|http://banatapp.com/Bannat_app/uploads/product/1543473823Color_Stick_M100.png|http://banatapp.com/Bannat_app/uploads/product/1543473823extremecleanser_44062_0.png|http://banatapp.com/Bannat_app/uploads/product/1543473823EYELINER.png
            [created_at] => 2018-11-29 06:43:43
            [updated_at] => 2018-11-29 06:43:43
        )

    [1] => stdClass Object
        (
            [id] => 13
            [user_id] => 5
            [products_categories_id] => 22
            [first_cat] => 1
            [sec_cat] => 3
            [price] => 20
            [title] => ARTIST STICK PENCIL
            [image] => http://banatapp.com/Bannat_app/uploads/product/1543476576Color_Stick_M100.png
            [description] => This isn't going to be some rah-rah, pumped-up motivational speech. (That's not my style.) Instead, we're going to break down the science behind how to get motivated in the first place and how to stay motivated for the long-run. Whether you're trying to figure out how to motivate yourself or how to motivate a team, this page should cover everything you need to know.

You can click the links below to jump to a particular section or simply scroll down to read everything. At the end of this page, you'll find a complete list of all the articles I have written on motivation.
            [sale] => 
            [qty] => 30
            [options] => http://banatapp.com/Bannat_app/uploads/product/1543476576AQUABROW.png|http://banatapp.com/Bannat_app/uploads/product/1543476576artist_rouge.png|http://banatapp.com/Bannat_app/uploads/product/1543476576brush_cleanser_50003_0.png|http://banatapp.com/Bannat_app/uploads/product/1543476576SCULPTINGBROW.png
            [created_at] => 2018-11-29 07:29:36
            [updated_at] => 2018-11-29 07:29:36
        )

    [2] => stdClass Object
        (
            [id] => 14
            [user_id] => 5
            [products_categories_id] => 22
            [first_cat] => 1
            [sec_cat] => 3
            [price] => 200
            [title] => EYELINER ARTIST COLOR
            [image] => http://banatapp.com/Bannat_app/uploads/product/1543476656EYELINER.png
            [description] => This isn't going to be some rah-rah, pumped-up motivational speech. (That's not my style.) Instead, we're going to break down the science behind how to get motivated in the first place and how to stay motivated for the long-run. Whether you're trying to figure out how to motivate yourself or how to motivate a team, this page should cover everything you need to know.

You can click the links below to jump to a particular section or simply scroll down to read everything. At the end of this page, you'll find a complete list of all the articles I have written on motivation.
            [sale] => 
            [qty] => 25
            [options] => http://banatapp.com/Bannat_app/uploads/product/1543476656SCULPTINGBROW.png|http://banatapp.com/Bannat_app/uploads/product/1543476656senseyes_44151_0.png|http://banatapp.com/Bannat_app/uploads/product/1543476656smoky_lash_23301_0.png|http://banatapp.com/Bannat_app/uploads/product/1543476656WATERBLEND.png
            [created_at] => 2018-11-29 07:30:56
            [updated_at] => 2018-11-29 07:30:56
        )

    [3] => stdClass Object
        (
            [id] => 15
            [user_id] => 5
            [products_categories_id] => 22
            [first_cat] => 1
            [sec_cat] => 3
            [price] => 100
            [title] => BRUSH CLEANSER BRUSH CLEANSER
            [image] => http://banatapp.com/Bannat_app/uploads/product/1543476725brush_cleanser_50003_0.png
            [description] => This isn't going to be some rah-rah, pumped-up motivational speech. (That's not my style.) Instead, we're going to break down the science behind how to get motivated in the first place and how to stay motivated for the long-run. Whether you're trying to figure out how to motivate yourself or how to motivate a team, this page should cover everything you need to know.

You can click the links below to jump to a particular section or simply scroll down to read everything. At the end of this page, you'll find a complete list of all the articles I have written on motivation.
            [sale] => 
            [qty] => 30
            [options] => http://banatapp.com/Bannat_app/uploads/product/1543476725POUCH.png|http://banatapp.com/Bannat_app/uploads/product/1543476725SCULPTINGBROW.png|http://banatapp.com/Bannat_app/uploads/product/1543476725senseyes_44151_0.png|http://banatapp.com/Bannat_app/uploads/product/1543476725WATERBLEND.png
            [created_at] => 2018-11-29 07:32:05
            [updated_at] => 2018-11-29 07:32:05
        )

    [4] => stdClass Object
        (
            [id] => 16
            [user_id] => 5
            [products_categories_id] => 22
            [first_cat] => 1
            [sec_cat] => 3
            [price] => 80
            [title] => WATER BLIND STICK COLOUR
            [image] => http://banatapp.com/Bannat_app/uploads/product/1543476879WATERBLEND.png
            [description] => This isn't going to be some rah-rah, pumped-up motivational speech. (That's not my style.) Instead, we're going to break down the science behind how to get motivated in the first place and how to stay motivated for the long-run. Whether you're trying to figure out how to motivate yourself or how to motivate a team, this page should cover everything you need to know.

You can click the links below to jump to a particular section or simply scroll down to read everything. At the end of this page, you'll find a complete list of all the articles I have written on motivation.
            [sale] => 
            [qty] => 90
            [options] => http://banatapp.com/Bannat_app/uploads/product/1543476879artist_rouge.png|http://banatapp.com/Bannat_app/uploads/product/1543476879LIPBRUSH.png|http://banatapp.com/Bannat_app/uploads/product/1543476879MASCARA.png|http://banatapp.com/Bannat_app/uploads/product/1543476879smoky_lash_23301_0.png
            [created_at] => 2018-11-29 07:34:39
            [updated_at] => 2018-11-29 07:34:39
        )

    [5] => stdClass Object
        (
            [id] => 17
            [user_id] => 5
            [products_categories_id] => 22
            [first_cat] => 1
            [sec_cat] => 3
            [price] => 110
            [title] => ARTIST ROUGE COLOR PENCIL
            [image] => http://banatapp.com/Bannat_app/uploads/product/1543476939artist_rouge.png
            [description] => This isn't going to be some rah-rah, pumped-up motivational speech. (That's not my style.) Instead, we're going to break down the science behind how to get motivated in the first place and how to stay motivated for the long-run. Whether you're trying to figure out how to motivate yourself or how to motivate a team, this page should cover everything you need to know.

You can click the links below to jump to a particular section or simply scroll down to read everything. At the end of this page, you'll find a complete list of all the articles I have written on motivation.
            [sale] => 
            [qty] => 25
            [options] => http://banatapp.com/Bannat_app/uploads/product/1543476939AQUABROW.png|http://banatapp.com/Bannat_app/uploads/product/1543476939artist_rouge.png|http://banatapp.com/Bannat_app/uploads/product/1543476939Color_Airbrush_M100.png|http://banatapp.com/Bannat_app/uploads/product/1543476939Color_Ink_M100.png|http://banatapp.com/Bannat_app/uploads/product/1543476939Color_Stick_M100.png
            [created_at] => 2018-11-29 07:35:39
            [updated_at] => 2018-11-29 07:35:39
        )

)

2 个答案:

答案 0 :(得分:1)

您的价格字段很可能是一个字符串,可以按数字(价格)订购,您应该将类​​型更改为数字字段,例如Decimal
让我知道我猜错了

答案 1 :(得分:1)

您可以通过添加零这样的东西来实现

SELECT * FROM SampleData ORDER BY data_char+0;

您还可以更改数据库中的数据类型以获得准确的结果。

  

Varchar   至   诠释

有关更多参考,请点击here

希望它对其他人也有帮助