仅在Laravel的Datetime中选择月份

时间:2016-08-09 08:59:27

标签: mysql laravel datetime select group-by

我仅从datetime选择月份部分时遇到问题。我在表格事务中有一个名为 payment_date 的列。我希望 payment_date select * from transactiongroup by个月。我试过这段代码:

$selectedYearTrx = $connectDB->table('transaction')                                  
    ->join('payment_channel','transaction.payment_channel_id','=','payment_channel.id') 
    ->select('id','payment_date', 'payment_channel_id','amount',DB::raw('MONTH(payment_date) as monthPayment'))
    ->whereYear('payment_date', '=', $year)
    ->where('status','=','S')
    ->wherein('payment_channel_id', [1,2,3])
    ->where('currency','=','IDR')
    ->orderBy('id', 'desc')
    ->get()
    ->groupBy('payment_channel_id','id','monthPayment');

但是,我在群组monthPayment中收到错误。错误是调用成员函数 groupby()

任何人都可以帮我解决这个问题吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

你应该在代码的末尾加上 - > get())

$selectedYearTrx = $connectDB->table('transaction')
                             ->join('payment_channel','transaction.payment_channel_id','=','payment_channel.id') 
                             ->select('id','payment_date', 'payment_channel_id','amount',DB::raw('MONTH(payment_date) as monthPayment'))
                             ->whereYear('payment_date', '=', $year)
                             ->where('status','=','S')
                             ->wherein('payment_channel_id', [1,2,3])
                             ->where('currency','=','IDR')
                             ->orderBy('id', 'desc')
                             ->get()
                             ->groupBy('payment_channel_id','id','monthPayment');

应该是

$selectedYearTrx = $connectDB->table('transaction')                                 
                             ->join('payment_channel','transaction.payment_channel_id','=','payment_channel.id') 
                             ->select('id','payment_date', 'payment_channel_id','amount',DB::raw('MONTH(payment_date) as monthPayment'))
                             ->whereYear('payment_date', '=', $year)
                             ->where('status','=','S')
                             ->wherein('payment_channel_id', [1,2,3])
                             ->where('currency','=','IDR')
                             ->orderBy('id', 'desc')
                             ->groupBy('payment_channel_id','id','monthPayment')->get();