【发布时间】:2019-10-21 10:21:13
【问题描述】:
一段时间以来,我一直试图弄清楚如何在我的 eloquent(收藏?)中实现自定义订单: 这是我的集合(注意我没有使用 ->get() 因为在函数的后半部分,我使用它来扩展查询/集合)
$ads = Ad::whereIn('ad_type',[2,3,1]); <-- This is my Desired Output as well.
我的广告表中有多个元素,每个 ad_type。
所以我试过了:
$ads1 = Ad::where('ad_type',1);
$ads2 = Ad::where('ad_type',2);
$mergedAds = $ads1->merge($ads2);
但我只是得到:
'Call to undefined method Illuminate\Database\Query\Builder::merge()'
接下来我尝试保留 $ads1 并添加:
$ads2 = sortBy(function($ads) use ($ad_types) {
return array_search($ads->getKey(), $ad_types);
});
但是,它不是一个数组,所以这不能工作。谁能把我引向正确的方向?或许可以解释一下 Collection、Eloquent Collection 和 Query 之间的区别,以便我能更好地理解这一点。 非常感谢你。 (为什么我问这个问题:我看到有人在做 collect() 或 new Collection() ,我不知道这是什么,我也没有真正发现任何有用的东西)
【问题讨论】:
标签: laravel collections eloquent