合并两个集合只会返回一个

时间:2019-05-21 17:00:19

标签: php laravel

我有两个laravel集合(发布和事件),我都将它们合并到一个新的名为slideItems的集合中,但是这个集合仅返回合并后的集合中的一个(在这种情况下,仅发布)。

$sliderPosts = Post::where('isFeatured', true)->where('isVisible', true)->with('postcategory')->latest()->get();
        //dump($sliderPosts);
        $sliderEvents = Event::where('isFeatured', true)->where('isVisible', true)->latest()->get();
        //dump($sliderEvents);
        $sliderItems = $sliderEvents->merge($sliderPosts)->take(4);
        //dump($sliderItems);

1 个答案:

答案 0 :(得分:0)

您要合并两个集合,并且仅获取4个对象,就这样

$sliderPosts = Post::where('isFeatured', true)->where('isVisible', true)->with('postcategory')->latest()->take(2);
//dump($sliderPosts);
$sliderEvents = Event::where('isFeatured', true)->where('isVisible', true)->latest()->take(2);
//dump($sliderEvents);
$sliderItems = $sliderEvents->merge($sliderPosts);
//dump($sliderItems);