gpt4 book ai didi

laravel - 为集合分页,Laravel

转载 作者:行者123 更新时间:2023-12-05 08:50:51 26 4
gpt4 key购买 nike

我尝试从 foreach 向每个用户添加一些新值,但因为我使用 get,现在我不能在响应中使用分页,但我还需要向每个用户添加这些值。有什么想法吗?

public function statistics()
{
$users = User::select(['id', 'name'])->get();
foreach ($users as $key => $user) {
$history = AnswerHistory::where('user_id', '=', $user->id)->get();
$user->total_votes = count($history);
$user->total_time = gmdate("H:i:s", ($history->sum('answer_time')));
}

return response()->json($users);
}

最佳答案

默认情况下,laravel 无法满足您的需求,但是您可以做一些事情。

方案一可以先返回paginator,再修改collection。

    $users = User::select(['id', 'name'])->paginate(4)->toArray();

$users['data'] = array_map(function ($user) {
$history = AnswerHistory::where('user_id', '=', $user->id)->get();
$user->total_votes = count($history);
$user->total_time = gmdate("H:i:s", ($history->sum('answer_time')));
return $user;
}, $users['data']);

return $users;

解决方法二 宏方式。如果您愿意,可以将 Collection 宏添加到服务提供者。这样你就可以在任何集合上调用 paginate() :有关示例实现,请参阅 AppServiceProvider.php。

    public function boot()
{
Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
}

然后你的代码会是这样

        $users = User::select(['id', 'name'])->get();
foreach ($users as $key => $user) {
$history = AnswerHistory::where('user_id', '=', $user->id)->get();
$user->total_votes = count($history);
$user->total_time = gmdate("H:i:s", ($history->sum('answer_time')));
}

return response()->json($users->paginate(4));

解决方案三 子类方式。在您想要一个不同于标准 Illuminate\Support\Collection 的“可分页”集合的地方,在您的应用程序中实现一个 Collection.php 的副本并简单地替换您的 在依赖文件的顶部使用 Illuminate\Support\Collection 语句 use App\Support\Collection:

<?php

namespace App\Support;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection as BaseCollection;

class Collection extends BaseCollection
{
public function paginate($perPage, $total = null, $page = null, $pageName = 'page')
{
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
}
}

你的代码将是这样的

// use Illuminate\Support\Collection
use App\Support\Collection;

$users = User::select(['id', 'name'])->get();
foreach ($users as $key => $user) {
$history = AnswerHistory::where('user_id', '=', $user->id)->get();
$user->total_votes = count($history);
$user->total_time = gmdate("H:i:s", ($history->sum('answer_time')));
}

return response()->json((new Collection($users))->paginate(4);

关于laravel - 为集合分页,Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61135037/

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