第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何對laravel中的對象數(shù)組進(jìn)行排序?

如何對laravel中的對象數(shù)組進(jìn)行排序?

PHP
白板的微信 2021-12-03 19:33:11
這是我的關(guān)系函數(shù)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);}當(dāng)我收到關(guān)系船的評論并返回時顯示[  {    "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"     }]我不想要它,我只想按created_at對其進(jìn)行排序,但它是這樣顯示的(這是字典嗎?)
查看完整描述

2 回答

?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個贊

要在應(yīng)用過濾器后刪除對象鍵sortByDesc(),您可以附加values()then all():


public function allComments($id)

{

    $id=(int)$id;

    $post=Post::find($id);

    $comments=$post->comments->sortByDesc('created_at')->values()->all();

    return $comments;

}


查看完整回答
反對 回復(fù) 2021-12-03
?
蠱毒傳說

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個贊

使用Laravel Collection 的values()功能。這將從結(jié)果集合中刪除鍵。


如果你想要一個數(shù)組而不是一個集合,請使用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;

}


查看完整回答
反對 回復(fù) 2021-12-03
  • 2 回答
  • 0 關(guān)注
  • 260 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號