通过ID数组对Laravel集合进行排序 [英] Sorting Laravel Collection via Array of ID's

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

问题描述

是否可以在仍通过关系访问的同时使用单独的ID数组对关系集合进行排序?

Checklist设置Checklist有许多ChecklistItem,相关项目的所需顺序作为属性Checklist::$item_order存在。它只是一个按用户所需顺序排列的数字ID数组。 :

class Checklist extends Model {
    protected $casts = ['item_order' => 'array'];

    public function items() {
        return $this->hasMany(ChecklistItem::class);
    }
}
class ChecklistItem extends Model {
    public function list() {
        return $this->belongsTo(Checklist::class);
    }
}

我以正常方式访问此关系:

$list = Checklist::find(1234);

foreach ($list->items as $item) {
    // More Code Here
}

是否有可行的方法根据$list->item_order数组中的值对$list->items进行排序?

(我不能只将"Order"列添加到"Item"表,b/c每个"List"的顺序都会更改。)

推荐答案

您可以这样做:

$order = $list->item_order;
$list->items->sortBy(function($model) use ($order){
    return array_search($model->getKey(), $order);
}

您还可以向模型添加一个属性访问器,执行相同的操作

public function getSortedItemsAttribute() 
{
    if ( ! is_null($this->item_order)) {
        $order = $this->item_order;

        $list = $this->items->sortBy(function($model) use ($order){
            return array_search($model->getKey(), $order);
        });
        return $list;
    }
    return $this->items;
}

用法:

foreach ($list->sortedItems as $item) {
    // More Code Here
}

如果您在多个地方需要这种功能,我建议您创建自己的Collection类:

class MyCollection extends IlluminateDatabaseEloquentCollection {

    public function sortByIds(array $ids){
        return $this->sortBy(function($model) use ($ids){
            return array_search($model->getKey(), $ids);
        }
    }
}
然后,要在您的模型中实际使用类覆盖newCollection()。在本例中,它将位于ChecklistItems类中:

public function newCollection(array $models = array())
{
    return new MyCollection($models);
}

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

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