how to save multiple query results in one array?

Posted 2 years ago by mindhunter

Hi guys. I want to get 5 last posts of each category in my website. So first of first i extracted all categories id from DB and now wants to loop over these ids to get last 4 post. Here is question: How can i save each query results in an array? is there any other logic or solution besides array? Thanks All. Here is my code:

$postsbycategory = array();
$categories = Category::all();
foreach ($categories as $category) {
        $x = Post::where('category_id' , $category->id);
        $postsbycategory = add $x into array???

Please sign in or create an account to participate in this conversation.