php - Laravel 5.4 集合 sortByDesc 不工作

标签 php laravel-5 collections

我有两个 Eloquent 查询,我收集并合并它们,然后我执行了 sortByDesc 但它不是排序集合。

$f_games = collect(Game::with('fUser', 'sUser')->where('first_user_id', Auth::user()->id)>get());
$s_games = collect(Game::with('fUser', 'sUser')->where('second_user_id', Auth::user()->id)->get());

$response = $f_games->merge($s_games)->sortByDesc('id');

最佳答案

您可以在排序结束时使用 values(),如 documentation 中所述。

$gameCollection = collect($game);

$sorted = $gameCollection->sortByDesc('date');

return $sorted->values()->all();

在你的情况下应该是

$response = $f_games->merge($s_games)->sortByDesc('id')->values();

关于php - Laravel 5.4 集合 sortByDesc 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48765120/

相关文章:

PHP fputcsv 生成空文件

php - 如何在 Blade 文件中的输入字段下方显示错误消息 - Laravel

javascript - laravel ajax 404页面未找到

java - LinkedList - 使用 "get()"的迭代器来更快地迭代

php - 如何使用 box.net API 上传文件?

php - 在自定义主页和产品类别文件中显示 WooCommerce 产品属性

javascript - 将 JavaScript 数组从 View 传递到 Laravel Controller

php - Laravel 5.4 通知中未找到类 'Illuminate\Mail\Events\MessageSent'

java - 集合 - 将元素添加到集合中

c# - Linq 返回过滤后的 child