如何从 Laravel 日期范围内未创建的数据库中获取记录?
Posted
技术标签:
【中文标题】如何从 Laravel 日期范围内未创建的数据库中获取记录?【英文标题】:How to get records from the database that wasn't created in the dates range in 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?
如何获得在日期范围内未售出的所有产品?
【问题讨论】:
【参考方案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);
【讨论】:
以上是关于如何从 Laravel 日期范围内未创建的数据库中获取记录?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 laravel 8 从 url 中获取动态 start_date 和 end_date(日期范围)?