Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-如何查询月份和年度日期?_Php_Laravel_Eloquent - Fatal编程技术网

Php Laravel-如何查询月份和年度日期?

Php Laravel-如何查询月份和年度日期?,php,laravel,eloquent,Php,Laravel,Eloquent,我是拉雷维尔的新手,不知道该怎么做 我在数据库中将日期记录为开始日在我的时间表中。 日期以以下格式保存:2019-10-03 现在我只有: $months = DB::table('times')->where('start_day', '=', 2019) 如何选择一年中每个月的数据?您需要将where()更改为whereYear()以按年份搜索 $months = DB::table('times')->whereYear('start_day', '=', 2019) 您需

我是拉雷维尔的新手,不知道该怎么做

我在数据库中将日期记录为
开始日
在我的
时间
表中。
日期以以下格式保存:
2019-10-03

现在我只有:

$months = DB::table('times')->where('start_day', '=', 2019)

如何选择一年中每个月的数据?

您需要将
where()
更改为
whereYear()
以按年份搜索

$months = DB::table('times')->whereYear('start_day', '=', 2019)

您需要将
where()
更改为
whereYear()
以按年份搜索

$months = DB::table('times')->whereYear('start_day', '=', 2019)

使用
whereYear

use Carbon\Carbon;

$months = DB::table('times')->whereYear('start_day', (string) Carbon::now()->year)->get();
如果只想选择月份,则

$months = DB::table('times')->select(DB::raw('MONTH(start_day) month')->whereYear('start_day', (string) Carbon::now()->year)->get();

使用
whereYear

use Carbon\Carbon;

$months = DB::table('times')->whereYear('start_day', (string) Carbon::now()->year)->get();
如果只想选择月份,则

$months = DB::table('times')->select(DB::raw('MONTH(start_day) month')->whereYear('start_day', (string) Carbon::now()->year)->get();

$month=array():
对于($i=1;$i whereyear('start_day','2019')->whereMonth('start_day',$i)->get();
}

$month=array():
对于($i=1;$i此处([['年(开始日)',=',2019],“'月(开始日)',=',(int)$i]])->get();
}
写入

$month = array():
for($i=1;$i<=12;$i++){
   $month[] = DB::table('times')->whereYear('start_day','2019')->whereMonth('start_day',$i)->get();
}
$month=array():
对于($i=1;$i whereyear('start_day','2019')->whereMonth('start_day',$i)->get();
}

$month=array():
对于($i=1;$i此处([['年(开始日)',=',2019],“'月(开始日)',=',(int)$i]])->get();
}

您可以使用此格式尝试每月、每周、每年

$weeklyNumberOfSales = Sale::whereBetween('order_date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
        ->sum('qty');

 $monthlyNumberOfSales = Sale::whereMonth('order_date',Carbon::now()->format('m'))
        ->sum('qty');

$yearlyNumberOfSales = Sale::whereYear('order_date',Carbon::now()->format('Y'))
        ->sum('qty');
型号名称销售
订单日期”和“数量”是字段名

您可以使用此格式尝试每月、每周、每年

$weeklyNumberOfSales = Sale::whereBetween('order_date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
        ->sum('qty');

 $monthlyNumberOfSales = Sale::whereMonth('order_date',Carbon::now()->format('m'))
        ->sum('qty');

$yearlyNumberOfSales = Sale::whereYear('order_date',Carbon::now()->format('Y'))
        ->sum('qty');
型号名称销售。 “订单日期”和“数量”是字段名