In your query take (5)
how to save multiple query results in one array?
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???
}
Sorry for the late reply I was away for work..
let solve the issue of latest post...
//here we want the 5 latest in each category
$eachCategoryWith5 = Category::all()->map(function ($category, $key){
return $category->posts->latest()->take(5);
});
//just use the latest method if you have created_at column in your post table
//if you don't have a created_at column in your table we can use the id of the post since it increments for each post we add, just fetch from below to top take the first 5 post
$eachCategoryWith5 = Category::all()->map(function ($category, $key){
return $category->posts->order('id','desc')->take(5);
});
okay let's start with Category::all()
this fetch all the categories we have, ->map() is a helper method in that enables you to do iterations on collections. Note that in L5.3 all eloquent models return Collections therefore we can call collections helper methods on them learn more at https://laravel.com/docs/5.3/collections.
function($category, $key) is the same as function($item, $key) Since we are iterating on the collection Category::all() we need to get each (in this case let say category in categories) item in the collection. That item at the current iterate is $category or $item and that should be the first parameter in the inner function() the $key is current index of the iterate..
Let's do this quick illustration
//LETS SAY Category::all() return the collection and assigned to $cat below
$cat = {[
{
id:1,
name:"Music"
created_at:"2017-01-17 19:43:47"
updated_at:"2017-01-17 19:43:47"
},
{
id:2,
name:"Health"
created_at:"2017-01-17 19:43:47"
updated_at:"2017-01-17 19:43:47"
},
{
id:3,
name:"Entertainment"
created_at:"2017-01-17 19:43:47"
updated_at:"2017-01-17 19:43:47"
}
]};
//then we perform iteration on $cat since we need 5 post for each of this category, we can't use foreach loop straight away since we are working with a collection, so we use the map() helper method to iterate.
//The map() helper method accepts an anonymous function as a parameter to hold off each iteration process, then return the item of the iteration process and the index of the item.. so in our case we are looking for a category in $cat so I'd pass $category as my item and $key as my index for the item
$cat->map(function($category,$key){
//in here is where we get post for each category then return it, and since we have an eloquent relationship we don't need to do more query logic here
/*
so for the first iterate on $cat
$category = {
id:1,
name:"Music"
created_at:"2017-01-17 19:43:47"
updated_at:"2017-01-17 19:43:47"
};
so we get all post with category_id of 1 , as per our relationship
*/
return $category->posts->latest()->take(5);
//since we are using the map() helper function, L5.3 automatically do the pushing for each iteration and at the end we get all the latest 5 post for each category.
});
Hope it helps
Please or to participate in this conversation.