laravel - 工具汇总
in Laravel with 0 条评论

laravel - 工具汇总

in Laravel with 0 comment

记录查询语句

AppServiceProvider boot 中添加

\DB::listen(
            function ($sql) {
                foreach ($sql->bindings as $i => $binding) {
                    if ($binding instanceof \DateTime) {
                        $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
                    } else {
                        if (is_string($binding)) {
                            $sql->bindings[$i] = "'$binding'";
                        }
                    }
                }

                // Insert bindings into query
                $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);

                $query = vsprintf($query, $sql->bindings);

                // Save the query to file
                $logFile = fopen(
                    storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'),
                    'a+'
                );
                fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL);
                fclose($logFile);
            }
        );

再查看 storage 下面的 *_query.log 表

图片描述...

查询指定时间区间

...
use Carbon\Carbon;
/**
 * 月统计
 */
public static function month()
{
    // 前5月的月份
    $first_month = Carbon::parse(Carbon::now()->subMonths(5)->format('Y-m'));

    // 往前面倒6个月
    $first = $first_month->format('Y-m-d');
    $second = $first_month->addMonths(1)->format('Y-m-d');
    $third = $first_month->addMonths(1)->format('Y-m-d');
    $fourth = $first_month->addMonths(1)->format('Y-m-d');
    $fifth = $first_month->addMonths(1)->format('Y-m-d');
    $sexth = $first_month->addMonths(1)->format('Y-m-d');
    $current = $first_month->addMonths(1)->format('Y-m-d');
    $times = compact('first', 'second', 'third', 'fourth', 'fifth', 'sexth', 'current');

    return $times;
}

/**
 * 查询指定时间范围的数据
 * @param $data
 * @return array
 * $type 1 首页访问量 2 app下载量 3 ios
 */
public static function between_total($data, $type =1)
{
    if(!$data) {
        return [];
    }
    $count = [];
    $count[0] = self::query()
        ->where('time', '>=', $data['first'])
        ->where('time', '<', $data['second'])
        ->where('type', $type)
        ->sum('count');
    ......

ab 测试

图片描述...

-n1000 表示总请求数位1000
-c 表示并发用户数为10
Server Software web 软件名称
图片描述...

图片描述...

Responses