【发布时间】:2019-02-19 15:39:58
【问题描述】:
我的 Laravel 控制器和排序有点问题:
public function index()
{
$achievements = Achievement::all();
$news = News::all();
$livingspaces = Livingspace::all();
$therapies = Therapy::all();
$events = Event::all();
$collection = collect();
foreach ($achievements as $achievement) {
$collection->push($achievement);
}
foreach ($news as $new) {
$collection->push($new);
}
foreach ($livingspaces as $livingspace) {
$collection->push($livingspace);
}
foreach ($events as $event) {
$collection->push($event);
}
foreach ($therapies as $therapy) {
$collection->push($therapy);
}
$sortedData = $collection->sortBy('category')->sortByDesc('created_at');
return response()->json([
'sortedData' => $sortedData
], 200);
}
根本没有排序。它应该在为 Laravel 控制器创建新迁移时开箱即用的 created_at 时间戳中的数据之后进行排序。但我无法对数据进行排序。我认为这与将数据库中的数据直接推送到集合中有关,它根本不寻找“created_at”。它没有给出任何错误或任何东西,它只是没有做任何事情。 sortBy 也是如此。
【问题讨论】:
标签: laravel controller laravel-5.7 laravel-controller