php - 如何在 Laravel 中获得不同的查询结果?

标签 php mysql laravel

我有一个用户表,其中有 Id 列。我需要获取 Id 列没有重复值的行。例如,这是我的表结构

ID - Name - Email
1--- xxx ------ xx
2--- xxx------- xx
2--- xxx ------ xx
1--- xxx------- xx

public function allUsers(Request $request)
{
    $this->updateOnlineStatus($request);

    $query = UsersList::query();

    $query->addSelect('users_list_view.user_type');

    $scoreDate = $request->get('score_date');
    if ($scoreDate &&
        (trim($scoreDate['from']) !== ''
            || trim($scoreDate['to']) !== '')
    ) {
        $query->selectSub(function ($q) use ($scoreDate) {
            $q->select(DB::raw('AVG(fortune_scores.score)*20 AS score'))
                ->from('fortune_scores')
                ->leftJoin('fortunes', 'fortunes.id', '=', 'fortune_scores.fortune_id')
                ->whereRaw('fortunes.user_id = users_list_view.id')
                ->distinct('id')
                ->get();


            if (trim($scoreDate['from']) !== '') {
                $scoreDate['from'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['from'] . ' 00:00:00');
                $q->where('fortune_scores.created_at', '>=', $scoreDate['from'])
                ->distinct('id')
                ->get();
            }

            if (trim($scoreDate['to']) !== '') {
                $scoreDate['to'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['to'] . ' 23:59:59');
                $q->where('fortune_scores.created_at', '<=', $scoreDate['to'])
                ->distinct('id')
                ->get();
            }

            return $q;

        }, 'percentage');

        $query->selectSub(function ($q) use ($scoreDate) {
            $q->select(DB::raw('COUNT(1) AS count'))
                ->from('fortunes')
                ->whereRaw('fortunes.user_id = users_list_view.id')
                ->whereIn('fortunes.status', [FortuneStatus::COMMENT_SENT, FortuneStatus::PENDING_ANSWER, FortuneStatus::ANSWER_SENT])
                ->distinct('id')
                ->get();
            if (trim($scoreDate['from']) !== '') {
                $scoreDate['from'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['from'] . ' 00:00:00');
                $q->where('fortunes.updated_at', '>=', $scoreDate['from'])
                ->distinct('id')
                ->get();
            }

            if (trim($scoreDate['to']) !== '') {
                $scoreDate['to'] = Carbon::createFromFormat('d/m/Y H:i:s', $scoreDate['to'] . ' 23:59:59');
                $q->where('fortunes.updated_at', '<=', $scoreDate['to'])
                ->distinct('id')
                ->get();
            }
        }, 'fortune_count');
    }


    $dataTable = new DataTableService($query, collect($request->except('score_date')));

    return $dataTable->setResultMap(function ($item) {
        $statusClass = $item->online_status == 0 ? 'danger' : ($item->online_status == 1 ? 'yellow-lemon' : 'green-jungle');
        $statusName = $item->online_status == 0 ? 'çevrimdışı' : ($item->online_status == 1 ? 'meşgul' : 'çevrimiçi');

        return [
            'id' => $item->id,
            'name' => $item->name,
            'online_status' => '<span class="label label-' . $statusClass . '">' . $statusName . '</span>',
            'balance' => ($item->balance ? $item->balance : 0) . " TL",
            'fortune_count' => ($item->user_type == UserType::RANDOM_TELLER ? User::find($item->id)->fortuneCount() : $item->fortune_count),
            'percentage' => number_format($item->percentage, 2) ?: '0',
            'credit' => $item->credit,
            'last_login' => $item->last_login ? Carbon::createFromFormat('Y-m-d H:i:s', $item->last_login)->format('d.m.Y H:i:s') : '',
            'action' => '<a href="' . route('admin.user.detail', ['id' => $item->id]) . '" class="btn btn-sm btn-outline grey-salsa" target="_blank"><i class="fa fa-search"></i> Detay</a>',
        ];
    })->get();

}

我尝试了“get、distinct、select、count”,但无法处理我的代码?

最佳答案

尝试在 ->get() 之后运行 ->unique()。是采集法。 https://laravel.com/docs/5.8/collections#method-unique

关于php - 如何在 Laravel 中获得不同的查询结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029707/

相关文章:

php - 如何在PHP错误验证期间将数据保留在表单中?

php - 如何在yii2中进行ajax调用?

php - 在 Laravel 中检索 updated_at 比 created_at 早 2 小时的记录( Eloquent )

laravel - 如何在 Laravel 中使用 ->append v. protected $appends

php - 如何将 Controller /操作设置为找不到页面

javascript - 脚本成功执行后运行php代码

PHP/MySQL 连接到单个列并更新表中的其他列

java - 通过服务管理与 mysql 的连接

MySql - 执行此类查询的最佳方法

php - Laravel Eloquent 的 API 适配器?