首页 > 解决方案 > 如何从 Laravel 中的日期按月份搜索

问题描述

我正在尝试这样:

$cost=DB::table('breakfast_orders')
      ->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
      ->where('breakfast_orders.user_id',1)
      ->WHERE(' MONTHNAME(date)='June'')
      ->sum('breakfast_costs.total_cost');

或者以下 sql 的转换可能对我有帮助:

SELECT * FROM `breakfast_orders` WHERE MONTHNAME(date)='June'

标签: sqllaravel

解决方案


$month = 'June';

$cost=DB::table('breakfast_orders')
      ->join('breakfast_costs','breakfast_orders.date','=','breakfast_costs.date')
      ->where('breakfast_orders.user_id',1)
      ->whereRaw('MONTH(date) = '.$month)
      ->sum('breakfast_costs.total_cost');

推荐阅读