将集合与过滤器返回模型对象一起使用

时间:2019-06-26 06:45:14

标签: laravel-5 collections

在我的laravel 5.7中,我将集合与过滤器一起使用,但是我想返回的不是设置为此过滤器的整数值,而是 根据过滤后的值对对象进行建模,例如:

$selectedSubscriptions = collect($tempSelectedSubscriptions)->filter(function ($item) {
    $nextTempSelectedSubscription = SiteSubscription::find($item);
    if ($nextTempSelectedSubscription !== null) {
        return (object)['id' => $nextTempSelectedSubscription->id, 'name' => $nextTempSelectedSubscription->name];
    }
});

我该怎么做?

0 个答案:

没有答案