Laravel集合中

时间:2015-11-24 22:18:25

标签: php json sorting laravel-5 laravel-5.1

我正在研究基于线程的消息传递系统。

我有一个简单的查询来返回特定线程的所有消息:

$t->messages->sortByDesc('updated_at')

将此传递给return response()->json()时,如果它有一条消息,则返回值为array。如果它有多个消息,则返回值为object{0 => {messageattribs}, 1 => {messageattribs}}个对象。

我想知道为什么会这样,以及为什么它没有像第一种情况那样没有索引键模式的数组。

1 个答案:

答案 0 :(得分:1)

默认情况下,Laravel会像您一样保留索引数组。

要“删除”索引,只需再调用几个方法:http://laravel.com/docs/5.1/collections#method-sortby

Alert alert = new Alert(AlertType.ERROR, "content text");
alert.getDialogPane().getChildren().stream().filter(node -> node instanceof Label).forEach(node -> ((Label)node).setMinHeight(Region.USE_PREF_SIZE));

它会像你想要的那样返回一个集合/集合。

希望它有所帮助!

祝福。

PS:让我邀请你参加一个Laravel课程,你可以在那里学到新东西:Laravel Course