Home > Software design >  How to get records from the database that wasn't created in the dates range in Laravel?
How to get records from the database that wasn't created in the dates range in Laravel?

Time:09-30

I have a function where I can get all sell records where between dates. Here is the function:

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

But now I need to create a function that will get all records from the database that didn't have any sales between the dates range.

Something like:

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

How can I get all products that didn't sell between the date range?

CodePudding user response:

You can simple use

whereNotBetween

CodePudding user response:

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);
  • Related