laravel 统计每日订单

按订单支付日期统计每日订单数量,需要用到 laravel 原始表达式。

有时候你可能需要在查询中使用原始表达式。这些表达式将会被当作字符串注入到查询中,所以要小心避免造成 SQL 注入攻击!要创建一个原始表达式,可以使用 DB::raw 方法:

 Order::query()
                ->selectRaw('DATE_FORMAT(paid_at,"%m-%d") as day')
                ->selectRaw('COUNT(*) as count')
                ->where('paid_at', '>', $time)
                ->where('paid_at', '<', date('Y-m-d 00:00:00', time()))
                ->groupBy('day')
                ->get()->toArray();

mysql 原生语句(转载)

SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * ) FROM testGROUP BY DATE_FORMAT( deteline, "%Y-%m-%d %H" ) 
查询某天:
deteline, "%Y-%m-%d
某时:
deteline, "%Y-%m-%d %H"
依次类推。
其实就是对dateline进行处理,然后再对处理后的数据分组

laravel自增或自减

查询构造器也为指定字段提供了便利的自增和自减方法 。此方法提供了一个比手动编写 update 语法更具表达力且更精练的接口。 这两个方法都必须接收至少一个参数(要修改的字段)。也可选择传入第二个参数,用来控制字段应递增/递减的量:

DB::table('users')->increment('votes');

DB::table('users')->increment('votes', 5);

DB::table('users')->decrement('votes');

DB::table('users')->decrement('votes', 5);

//在指定要操作中更新其它字段:
DB::table('users')->increment('votes', 1, ['name' => 'John']);