gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:46:03 26 4
gpt4 key购买 nike

我有一个用户表,其中有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com