Laravel-Admin Tab 分栏按日月年统计新增用户

yufei       2 周, 1 天 前       33

Laravel-Admin Tab 分栏按日月年统计新增用户

折腾了一天,懒得写说明了,大家自己用着吧

<?php

namespace App\Admin\Controllers\Tj;

use Auth;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Layout\Content;
use App\Admin\Actions\Product\JumpApps;

use DB;
use App\Models\Tj\TjUser;

class TjUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户统计';

    public function index(Content $content)
    {
        $tab = new Tab();

        $tt = request()->input('tt','dayly');

        foreach(['daily' => '每日新增','month' => '每月新增','year' => '每年新增'] as $item => $title) 
        {
            if($tt == $item) {
                $tab->add($title, $this->grid($tt,$title)->render(),true); 
            } else {
                $tab->addLink($title, '/root/tjuser?tt='.$item);
            }
        }

        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body($tab);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($tt = 'daily')
    {
        $model = new TjUser;
        $grid = new Grid($model);
        $labels = ['daily'=>'日期','month'=>'月份','year'=>'年份'];
        $grid->model()->groupBy('item')->orderBy('item','desc');
        if($tt == 'daily')
        {
            $grid->model()->select([DB::raw("count(*) as total,date_format(created_at,'%Y-%m-%d') as item")]);
        } else if ($tt == 'month'){
            $grid->model()->select([DB::raw("count(*) as total,date_format(created_at,'%Y-%m') as item")]);
        } else {
            $grid->model()->select([DB::raw("count(*) as total,date_format(created_at,'%Y') as item")]);

        }


        $grid->column('item',$labels[$tt]);
        $grid->column('total','新增用户');
        $grid->filter(function($filter){
            $filter->between('created_at','时间')->datetime(['format'=>'YYYY-MM-DD 00:00:00']);
        });

        $grid->export(function ($export) use ($tt) {
            $export->filename(['daily'=>'按日统计每日新增用户','month'=>'按月统计每日新增用户','year'=>'按年统计每日新增用户'][$tt].date('_Y-m-d\TH:i:s'));
        });


        $grid->disableCreation();
        $grid->disableActions();
        $grid->disableRowSelector();
        $grid->disableColumnSelector();
        return $grid;
    }
}
目前尚无回复
简单教程 = 简单教程,简单编程
简单教程 是一个关于技术和学习的地方
现在注册
已注册用户请 登入
关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2018 简单教程 twle.cn All Rights Reserved.