1

I have this Eloquent query:

$events = Product::forCard()->latest()->take(setting('storefront_recent_products_section_total_products', 10))->get();

I want to sort this record in ascending order by start_event. I tried to do this, but I have some issues because data come from multiple tables.

Array
(
    [0] => Array
        (
            [price] => Modules\Support\Money Object
                (
                    [amount:Modules\Support\Money:private] => 
                    [currency:Modules\Support\Money:private] => USD
                )

            [special_price] => 
            [selling_price] => Modules\Support\Money Object
                (
                    [amount:Modules\Support\Money:private] => 
                    [currency:Modules\Support\Money:private] => USD
                )

            [special_price_start] => 
            [special_price_end] => 
            [options_count] => 0
            [id] => 40
            [slug] => -YvHm5m1m
            [in_stock] => 1
            [new_from] => 
            [new_to] => 
            [name] => Race4
            [description] => 
            [organizer] => 
            [short_description] => 
            [address] => 
            [city] => vehari
            [state] => pakistan
            [zip_code] => 
            [lat] => 
            [lng] => 
            [start_event] => 2020-01-13 00:00:00
            [end_event] => 
            [translations] => Array
                (
                    [0] => Array
                        (
                            [id] => 33
                            [product_id] => 40
                            [locale] => en
                            [name] => Race4
                            [city] => vehari
                            [state] => pakistan
                            [start_event] => 2020-01-13 00:00:00
                        )

                )

            [files] => Array
                (
                )

        )
2
  • 1
    "I have some issues" is not a problem description. What output do you get? Why is that wrong? What should it be instead? Commented Jan 10, 2020 at 11:35
  • 1
    Have you tried the orderBy option ? Commented Jan 10, 2020 at 11:38

2 Answers 2

2

You should be able to order is ascending something similiar to this:

$events = Product::forCard()->latest()
                            ->take(setting('storefront_recent_products_section_total_products', 10))
                            ->orderBy('start_event', 'asc');

Basically you can tell using the method orderBy() which column shall be used to order and the type of ordering, in this case ascending.

Sign up to request clarification or add additional context in comments.

Comments

0

Laravel makes it easy and absolutely no brainer to sort data.

$events = Product::forCard()->latest()->take(setting('storefront_recent_products_section_total_products', 10))
->orderBy('start_event')->get();

Where start_event is the column you want t use in sorting.

There is a better approach, as the above code will automatically sort the returned data in an ascending order, but you can explicitly tell it what to do.

Just modify the last part of the code with:

orderBy('start_event', 'ASC')->get(); /** If you want Ascending OR; **/

orderBy('start_event', 'DESC')->get(); /** If you want Descending. **/

1 Comment

data is sorted on the base of primary key but i want to sort on start_event, so thankx for guideline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.