如何在Laravel中对对象数组进行排序?

时间:2019-08-25 04:22:23

标签: php laravel sorting vue.js

这是我的关系函数

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 对其进行排序,但是这样显示(这是字典吗?)

3 个答案:

答案 0 :(得分:0)

您可以尝试这种类型的声明

collect($yourArray)->sortBy('Key','DESC')->pluck('name');

您可以这样使用。.

  public function allComments($id)
{
    $id=(int)$id;
    $post=Post::find($id);
    $comments=$post->comments->sortByDesc('created_at')->values()->all();
    return $comments;
}

答案 1 :(得分:0)

要在应用sortByDesc()之类的过滤器后删除对象键,可以先附加values()然后附加all()

public function allComments($id)
{
    $id=(int)$id;
    $post=Post::find($id);
    $comments=$post->comments->sortByDesc('created_at')->values()->all();
    return $comments;
}

答案 2 :(得分:0)

使用 Laravel集合values()功能。这将从结果集合中删除键。

如果您想要一个数组而不是一个集合,请使用values()->all()

public function allComments($id)
{
    $id = (int) $id;
    $post = Post::find($id);
    $comments = $post->comments
                    ->sortByDesc('created_at')
                    ->values()
                    ->all();

    return $comments;
}

建议

更流畅的代码。

public function allComments($id)
{
    $comments = Comment::where('pots_id', $id)
                    ->sortByDesc('created_at')
                    ->values()
                    ->all();

    return $comments;
}
相关问题