【问题标题】:Update Status when each order time past 20 minutes每个订单时间超过 20 分钟时更新状态
【发布时间】:2016-07-28 13:13:12
【问题描述】:

我使用 Laravel Lumen 框架和 jenssegers/laravel-mongodb 包,我在项目中的查询是:

    $time_5_min_ago = Carbon::now()->subMinute(5);
    $time_10_min_ago = Carbon::now()->subMinute(10);
    $time_15_min_ago = Carbon::now()->subMinute(15);
    $time_20_min_ago = Carbon::now()->subMinute(20);



    return Order::where(function ($query)  use ($maxLat_try_one,$minLat_try_one,$maxLon_try_one,$minLon_try_one,$time_5_min_ago,$time_10_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_one, $maxLon_try_one])
            ->whereBetween('source_latitude', [$minLat_try_one,$maxLat_try_one])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_5_min_ago)
            ->where('created_at', '>=', $time_10_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_two,$minLat_try_two,$maxLon_try_two,$minLon_try_two,$time_10_min_ago,$time_15_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_two, $maxLon_try_two])
            ->whereBetween('source_latitude', [$minLat_try_two,$maxLat_try_two])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_10_min_ago)
            ->where('created_at', '>=', $time_15_min_ago);
    })->orWhere(function ($query)  use ($maxLat_try_three,$minLat_try_three,$maxLon_try_three,$minLon_try_three,$time_15_min_ago,$time_20_min_ago) {
        $query->whereBetween('source_longitude', [$minLon_try_three, $maxLon_try_three])
            ->whereBetween('source_latitude', [$minLat_try_three,$maxLat_try_three])
            ->where('status', '=', 'pending')
            ->where('created_at', '<', $time_15_min_ago)
            ->where('created_at', '>=', $time_20_min_ago);
    })->get($fields);

我想在顶部查询中存在任何订单,并且在最后一个 orWehere 查询中,订单 created_at 是

【问题讨论】:

    标签: php mongodb laravel lumen jenssegers-mongodb


    【解决方案1】:

    我认为你应该使用 Eloquen 属性而不是每分钟更新数据库。

    public function getStatusAttribute() {
     if($this->created > ....) {
       $status = "pending";
     } else if(...) {
       ....
     }
     return $status;
    }
    

    【讨论】:

    • 抱歉,您可以添加更详细的说明,说明必须做什么。因为我从来没有做过这件事
    【解决方案2】:

    结合 schedule 使用 Laravel 的命令,你会得到你想要的。

    这里有详细的解释:

    第 1 步:创建一个名为“ChangePendingToSuspended”的命令

    打开控制台然后执行

    php artisan make:console ChangePendingToSuspended
    

    第二步:打开ChangePendingToSuspended.php

    您可以在您的app/Console/Commands/ 目录中找到它并篡改它的参数,例如添加描述

    protected $description = 'Changes the Requests which has been in pending status for a period of time to suspended status.';
    

    和一个签名

    protected $signature = 'requests:clear-pending';
    

    好的,在你问“什么是签名?”之前 签名是从控制台执行命令的一种方式,例如现在您可以手动启动来自 artisan 的 ChangePendingToSuspended 命令,例如

    php artisan requests:clear-pending
    

    第 3 步:定义我们的命令

    现在您将代码放入 handle 方法中,在您的情况下,它可能是以下上下文中的内容:

    public function handle(){
        \DB::table('requests')
            ->where('created_at','<',\Carbon\Carbon::now()->addMinutes(-20))
            ->update(['status'=>'suspended']);
    }
    

    只需使用您喜欢的任何方法来更改该命令中的状态。

    第 4 步:将命令添加到计划中

    打开在app\Console\ 目录中找到的Kernel.php 您将看到一个名为 $commands 的数组,将我们的类添加到其中

     protected $commands = [
            Commands\Inspire::class,
            Commands\ChangePendingToSuspended::class,
        ];
    

    现在转到 schedule 方法并安排您新创建的命令

    protected function schedule(Schedule $schedule)
        {
    ...
            $schedule->command('requests:change-pending-to-investigate')->everyFiveMinutes();
    ...
        }
    

    好的,现在,每五分钟,调度程序将每五分钟执行一次我们的命令ChangePendingToSuspended, 但是还有 1 步,我们需要通过将它的 cron 作业添加到我们的系统来完成计划。

    第 5 步:将计划 cron 条目添加到您的服务器

    这在服务器和版本之间有所不同,无论您使用的是 windows、linux 还是 osx

    但这是 cron 条目

    对于 Linux:

    * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
    

    对于 Windows(使用任务调度程序):

    * * * * path/to/php path/to/artisan schedule:run 1>> NUL 2>&1
    

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2020-01-23
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2016-10-09
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多