【问题标题】:How to get records from the database that wasn't created in the dates range in Laravel?如何从 Laravel 日期范围内未创建的数据库中获取记录?
【发布时间】:2021-11-22 01:55:25
【问题描述】:

我有一个功能,我可以在其中获取日期之间的所有销售记录。这是函数:

private function getSoldsBetweenDates($days, $user, $filter_by)
    {
        $date_from = Carbon::now()->subDays($days);
        $date_to = Carbon::now();

        return Inventory::where('inventory.client_id', $user->client_id)
                                ->withCount(["sellRecord as $filter_by"  => function($query) {
                                    $query->select(DB::raw("created_at"))->take(1);
                                }])
                                ->join('inventory_sell_records', 'inventory_sell_records.product_id', '=', 'inventory.id')
                                ->groupBy('inventory_sell_records.product_id')
                                ->whereBetween('inventory_sell_records.created_at', [$date_from, $date_to])
                                ->paginate(100);
    }

但现在我需要创建一个函数,该函数将从数据库中获取在日期范围内没有任何销售的所有记录。

类似:

private function getDidntSellBetweenDates($days, $user, $filter_by)
    {
        What should I do here?
    }

如何获得在日期范围内未售出的所有产品?

【问题讨论】:

    标签: php mysql laravel eloquent laravel-relations


    【解决方案1】:

    你可以简单使用

    whereNotBetween
    

    【讨论】:

      【解决方案2】:
      return Inventory::where('inventory.client_id', $user->client_id)
                                      ->withCount(["sellRecord as $filter_by"  => function($query) {
                                          $query->select(DB::raw("created_at"))->take(1);
                                      }])
                                      ->join('inventory_sell_records', 'inventory_sell_records.product_id', '=', 'inventory.id')
                                      ->groupBy('inventory_sell_records.product_id')
                                      ->where('inventory_sell_records.created_at','<', $date_from)
                                      ->where('inventory_sell_records.created_at','>', $date_to)
                                      ->paginate(100);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多