How do I get a “select count(*) group by” using laravel eloquent

后端 未结 3 2246
野性不改
野性不改 2021-02-20 01:49

I would like to execute the follow sentence using laravel eloquent

SELECT *, count(*) FROM reserves  group by day

The only solution occurs to m

相关标签:
3条回答
  • 2021-02-20 02:16

    As you wish to do it with Laravel Eloquent I assume you have a model name Reserve. In this case you can use this

    $reserve = Reserve::all()->groupBy('day')->count();
    
    0 讨论(0)
  • 2021-02-20 02:24

    You could use:

    #Laravel Raw Expressions
    
      $reserves = DB::table('reserves')
                           ->select(DB::raw('count(*) as reserves_count'))           
                           ->groupBy('day')
                           ->get();
    
    OR
    
      $reserves = Reserve::select(['reserves.*'])
                           ->groupBy('day')
                           ->count();
    

    Further read here

    0 讨论(0)
  • 2021-02-20 02:34

    You could use this:

    $reserves = DB::table('reserves')->selectRaw('*, count(*)')->groupBy('day');
    
    0 讨论(0)
提交回复
热议问题