用户列表数据

Posted sgm4231

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户列表数据相关的知识,希望对你有一定的参考价值。

/**
     * 获取用户列表数据.
     *
     * @param Request $request
     * @return mixed
     * @author Seven Du <[email protected]>
     */
    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);
    }

以上是关于用户列表数据的主要内容,如果未能解决你的问题,请参考以下文章

相同的片段与不同选项卡上的列表视图

如何在片段内的 recylerview 列表中显示 SQLite 数据库数据?

两个片段之间的 onItemClickListener

从片段访问数据库以在列表中显示

如何将列表视图中的数据从一个片段发送到另一个片段

片段中的 notifyDataSetChanged() 不刷新列表视图