Laravel 5.5 - 按月获取用户和组

时间:2018-02-19 20:06:05

标签: laravel

我正在尝试从Laravel 5.5数据库中返回所有用户,并按照他们这样注册的月份对它们进行分组..

class AddAttachmentThumbnailToDatabanks < ActiveRecord::Migration[4.2]
  def self.up
    change_table :databanks do |t|
      t.attachment :thumbnail
    end
  end

  def self.down
    remove_attachment :databanks, :thumbnail
  end
end

但是这给了我一个错误

$users = DB::table('users')
     ->orderBy('created_at')
     ->groupBy(DB::raw("MONTH(created_at)"))
     ->get();

我哪里错了?

**更新**

我得到的确切错误消息是......

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column

1 个答案:

答案 0 :(得分:0)

您可以尝试:

$users = DB::table('users')
->select('name','email',DB::raw('MONTH(created_at) month'))
->orderBy('created_at', 'asc')
->groupby('month')
->get();
相关问题