Laravel orderBy created_at和另一个时间字段

时间:2018-05-17 12:06:28

标签: laravel

我有以下表格和以下代码来检索商店订单;

我的目的是根据创建的时间获取订单并订购它们,并按照升序顺序订购delivery_from时间字段,同时属于created_at orderby。

有效查询的示例将获取以下内容;

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

我目前的代码就是这样取而代之的;

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();
        });

1 个答案:

答案 0 :(得分:2)

我认为您应该首先按delivery_from订购,如下所示:

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