Laravel 5.1-对合并的Eloquent集合进行排序 [英] Laravel 5.1 - Sorting merged Eloquent Collections

查看:45
本文介绍了Laravel 5.1-对合并的Eloquent集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我合并了两个Eloquent集合以产生以下输出:

I have merged two Eloquent collections to produce the following output:

[
    "2015-03-05 21:54:19" => 100
    "2015-07-05 18:54:00" => 4
    "2015-06-30 21:53:29" => 3
]

第一列是在时间戳记中更新的,最后一列是两个表的ID.

Where the first column is updated_at timestamp and the last column is the IDs for two tables.

我的代码如下:

$jobs = $this->user->find($userId)->jobs()->lists('id', 'updated_at');
$bids = $this->user->find($userId)->bids()->lists('id', 'updated_at');

$all = $jobs->merge($bids)->sortByDesc('updated_at');

dd($all);

我的问题是,如何对数组进行排序,以使它们按updated_at列的降序排列?

My question is, how can I sort the arrays so that they're ordered in descending order by the updated_at column?

推荐答案

这对我来说很好降序排列.$ all = $ jobs-> merge($ bids)-> sortByDesc('updated_at');

This worked fine for me For descending order. $all = $jobs->merge($bids)->sortByDesc('updated_at');

对于升序.$ all = $ jobs-> merge($ bids)-> sortBy('updated_at');

For ascending order. $all = $jobs->merge($bids)->sortBy('updated_at');

这篇关于Laravel 5.1-对合并的Eloquent集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆