码迷,mamicode.com
首页 > 其他好文 > 详细

用户列表数据

时间:2019-03-20 14:31:08      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:current   opera   cti   pre   nat   esc   orderby   datetime   new   

/**
     * 获取用户列表数据.
     *
     * @param Request $request
     * @return mixed
     * @author Seven Du <shiweidu@outlook.com>
     */
    public function users(Request $request)
    {
        if (! $request->user()->ability(‘admin:user:show‘)) {
            return response()->json([
                ‘errors‘ => [‘你没有权限查管理用户‘],
            ])->setStatusCode(403);
        }

        $sort = $request->query(‘sort‘);
        $userId = $request->query(‘userId‘);
        $email = $request->query(‘email‘);
        $name = $request->query(‘name‘);
        $phone = $request->query(‘phone‘);
        $role = $request->query(‘role‘);
        $perPage = $request->query(‘perPage‘, 20);
        $showRole = $request->has(‘show_role‘);
        $follow = $request->query(‘follow‘, 0);
        $registStartDate = $request->query(‘regist_start_date‘);
        $registEndDate = $request->query(‘regist_end_date‘);
        $location = array_filter(explode(‘ ‘, $request->query(‘location‘) ?? ‘ ‘));

        $builder = with(new User())->setHidden([])->newQuery();

        $datas = [];
        if ($showRole) {
            $datas[‘roles‘] = Role::all();
        }
        // user id
        if ($userId && $users = $builder->where(‘id‘, $userId)->paginate($perPage)) {
            $datas[‘users‘] = $users->map(function ($user) {
                $user->setHidden([]);
                $user->load(‘recommended‘);
                $user->load(‘famous‘);

                return $user;
            });

            $datas[‘page‘][‘last_page‘] = $users->lastPage();
            $datas[‘page‘][‘current_page‘] = $users->currentPage();
            $datas[‘page‘][‘total‘] = $users->total();

            return response()->json($datas)->setStatusCode(200);
        }

        foreach ([
            ‘email‘ => [
                ‘operator‘ => ‘like‘,
                ‘value‘ => sprintf(‘%%%s%%‘, $email),
                ‘condition‘ => boolval($email),
            ],
            ‘name‘ => [
                ‘operator‘ => ‘like‘,
                ‘value‘ => sprintf(‘%%%s%%‘, $name),
                ‘condition‘ => boolval($name),
            ],
            ‘phone‘ => [
                ‘operator‘ => ‘like‘,
                ‘value‘ => sprintf(‘%%%s%%‘, $phone),
                ‘condition‘ => boolval($phone),
            ],
            ‘location‘ => [
                ‘operator‘ => ‘like‘,
                ‘value‘ => sprintf(‘%%%s%%‘, end($location)),
                ‘condition‘ => boolval(end($location)),
            ],
        ] as $key => $data) {
            if ($data[‘condition‘]) {
                $builder->where($key, $data[‘operator‘], $data[‘value‘]);
            }
        }

        // 注册时间
        $builder->when(boolval($registStartDate), function ($query) use ($registStartDate) {
            $query->where(‘created_at‘, ‘>=‘, Carbon::parse($registStartDate)->startOfDay()->toDateTimeString());
        });

        $builder->when(boolval($registEndDate), function ($query) use ($registEndDate) {
            $query->where(‘created_at‘, ‘<=‘, Carbon::parse($registEndDate)->endOfDay()->toDateTimeString());
        });

        // build sort.
        $builder->orderBy(‘id‘, ($sort === ‘down‘ ? ‘desc‘ : ‘asc‘));

        $role && $builder->whereHas(‘roles‘, function ($query) use ($role) {
            $query->where(‘id‘, $role);
        });

        $follow && $builder->whereHas(‘famous‘, function ($query) use ($follow) {
            // 检索被关注
            if ($follow == 1) {
                $query->where(‘type‘, ‘like‘, ‘followed‘);
            }
        });

        $pages = $builder->paginate($perPage);

        $datas[‘users‘] = $pages->map(function ($user) {
            $user->setHidden([]);
            $user->load(‘recommended‘);
            $user->load(‘famous‘);

            return $user;
        });

        $datas[‘page‘][‘last_page‘] = $pages->lastPage();
        $datas[‘page‘][‘current_page‘] = $pages->currentPage();
        $datas[‘page‘][‘total‘] = $pages->total();

        $year = $request->get(‘years‘);
        $datas[‘day_count‘]=User::whereDate(‘created_at‘,date(‘Y-m-d‘,time()))->count();
        $datas[‘month_count‘]=User::whereMonth(‘created_at‘,date(‘m‘,time()))->count();
        $datas[‘year_count‘]=User::when($year,function ($query) use ($year){
            return $query->whereYear(‘created_at‘,$year);
        })->count();

        return response()->json($datas)->setStatusCode(200);
    }

用户列表数据

标签:current   opera   cti   pre   nat   esc   orderby   datetime   new   

原文地址:https://www.cnblogs.com/sgm4231/p/10564635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!