0

I have the following table and and the following code to retrieve a store orders;

My intention was to fetch orders and order them according to the created at and also order the delivery_from time field in an ascending order while pertaining the created_at orderby.

An example a valid query would fetch the following;

delivery_from: 10:00, created_at: 10/10/2018
delivery_from: 12:00, created_at: 10/10/2018
delivery_from: 09:00, created_at: 15/10/2018
delivery_from: 15:00, created_at: 15/10/2018

My current code below fetches it like so;

delivery_from: 09:00, created_at: 15/10/2018
delivery_from: 10:00, created_at: 10/10/2018
delivery_from: 12:00, created_at: 10/10/2018
delivery_from: 15:00, created_at: 15/10/2018

$store = auth('manager')->user->store;
$orders = $store->orders()->orderBy('created_at', 'desc')->orderBy('delivery_from', 'asc')->get();

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('store_id');
            $table->unsignedInteger('delivery_method');
            $table->float('final_price', 8, 2);
            $table->float('delivery_price', 8, 2);
            $table->unsignedInteger('payment_type');
            $table->unsignedInteger('status');
            $table->boolean('paid')->default(false);
            $table->time('delivery_from');
            $table->time('delivery_to');
            $table->timestamps();
        });
Paul Diamant
  • 189
  • 2
  • 13
  • Check this out https://stackoverflow.com/questions/34602229/laravel-5-1-order-by-two-columns-not-working-as-intended – Indra May 17 '18 at 12:13

1 Answers1

2

I think you should order by delivery_from first, like this:

$orders = $store->orders()->orderBy('delivery_from', 'asc')->orderBy('created_at', 'desc')->get();
JCPHPDev
  • 169
  • 5