php - 如何对laravel中的对象数组进行排序?
问题描述
这是我的关系函数
public function comments()
{
return $this->hasMany(Comment::class);
}
这是我的控制器
public function allComments($id)
{
$id=(int)$id;
$post=Post::find($id);
$comments=$post->comments;
return dd($comments);
}
当我收到关系船的评论并返回时显示
[
{
"id":48,
"user_id":1,
"post_id":17,
"isactive":1,
"body":"s",
"created_at":"2019-08-24 02:53:54",
"updated_at":"2019-08-24 02:53:54"},
{
"id":78,
"user_id":1,
"post_id":17,
"isactive":1,
"body":"s",
"created_at":"2019-08-24 02:54:06",
"updated_at":"2019-08-24 02:54:06"
},
{
"id":79,
"user_id":1,
"post_id":17,
"isactive":1,
"body":"s",
"created_at":"2019-08-24 02:54:06",
"updated_at":"2019-08-24 02:54:06"
},
{
"id":80,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u06a9\u06cc\u0631 \u062e\u0631\u06cc",
"created_at":"2019-08-25 06:48:51",
"updated_at":"2019-08-24 06:48:51"
},
{
"id":102,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0628",
"created_at":"2019-08-25 01:32:39",
"updated_at":"2019-08-25 01:32:39"
},
{
"id":103,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0645\u0646 \u0628\u0627\u06cc\u062f \u0627\u0648\u0644 \u0628\u0627\u0634\u0645",
"created_at":"2019-08-25 01:35:13",
"updated_at":"2019-08-25 01:35:13"
},
{
"id":104,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0645\u0646 \u0627\u0648\u0644\u0645",
"created_at":"2019-08-25 02:01:32",
"updated_at":"2019-08-25 02:01:32"
},
{
"id":105,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0627\u0648\u0644 \u0634\u062f\u0645",
"created_at":"2019-08-25 02:02:18",
"updated_at":"2019-08-25 02:02:18"
}
]
然后当我更改控制器并在其上使用 sortByDesc
public function allComments($id)
{
$id=(int)$id;
$post=Post::find($id);
$comments=$post->comments->sortByDesc('created_at');;
return $comments;
}
结果是:
{
"3": {
"id":80,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u06a9\u06cc\u0631 \u062e\u0631\u06cc",
"created_at":"2019-08-25 06:48:51",
"updated_at":"2019-08-24 06:48:51"
},
"7": {
"id":105,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0627\u0648\u0644 \u0634\u062f\u0645",
"created_at":"2019-08-25 02:02:18",
"updated_at":"2019-08-25 02:02:18"
},
"6": {
"id":104,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0645\u0646 \u0627\u0648\u0644\u0645",
"created_at":"2019-08-25 02:01:32",
"updated_at":"2019-08-25 02:01:32"
},
"5": {
"id":103,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0645\u0646 \u0628\u0627\u06cc\u062f \u0627\u0648\u0644 \u0628\u0627\u0634\u0645",
"created_at":"2019-08-25 01:35:13",
"updated_at":"2019-08-25 01:35:13"
},
"4": {
"id":102,
"user_id":2,
"post_id":17,
"isactive":1,
"body":"\u0628",
"created_at":"2019-08-25 01:32:39",
"updated_at":"2019-08-25 01:32:39"
},
"1": {
"id":78,
"user_id":1,
"post_id":17,
"isactive":1,
"body":"s",
"created_at":"2019-08-24 02:54:06",
"updated_at":"2019-08-24 02:54:06"
},
"2": {
"id":79,
"user_id":1,
"post_id":17,
"isactive":1,
"body":"s",
"created_at":"2019-08-24 02:54:06",
"updated_at":"2019-08-24 02:54:06"},
"0": {
"id":48,
"user_id":1,
"post_id":17,
"isactive":1,
"body":"s",
"created_at":"2019-08-24 02:53:54",
"updated_at":"2019-08-24 02:53:54"
}
}
我不想要它,我只想按created_at对其进行排序,但它是这样显示的(这是这本字典吗?)
解决方案
要在应用过滤器后删除对象键,sortByDesc()
您可以附加: values()
all()
public function allComments($id)
{
$id=(int)$id;
$post=Post::find($id);
$comments=$post->comments->sortByDesc('created_at')->values()->all();
return $comments;
}
推荐阅读
- docker - docker-compose 如何使用相同的图像创建多个容器
- amazon-web-services - 将 AWS Code Commit 与 Microsoft AD 作为联合提供者并跨多个 AWS 账户使用
- iis - IIS 服务器 url 重写
- python - 如何将调试库“python37_d.lib”带到 Anaconda?
- python - 遍历框架中的所有子小部件并检查小部件的类型
- web-services - 寻找示例或指南或链接以了解 JAX-WS 1.2 项目
- listview - 小部件进入列表视图或颤动中的数据
- reactjs - 包括 id 的 ReactJs 路由路径返回不正确的组件
- c++ - 如何防止 CMAKE 在构建时为共享库创建符号链接(不在安装时)
- github - 仅在阻止拉取请求时手动运行 GitHub Actions