Crazy Coders
Crazy Coders

Reputation: 133

Show posts from each category like mixed in same list

i am using Laravel 5.2 for my project.

I have a list that shows posts. I have 2 different categories. I'm showing them in a list with foreach loop. Every category in that list fetches 6 post from model. What i want to do is , showing them in one list like they mixed. One from first category one from second category.

Example List:

- This post from News Model 
- This post from Blog Model 
- This post from News Model 
- This post from Blog Model 
- This post from News Model 
- This post from Blog Model 

How can i achieve this , thank you !

Upvotes: 2

Views: 52

Answers (1)

Eli
Eli

Reputation: 984

Well I can suggest you an idea but is base in just php as I have no idea of your code and I am not a Laravel user. But I guess could have two arrays and access then in a for loop simultaneously.

$news = array() // fill this array with your news posts
$blog = array() // fill this one with your blog posts

for($i = 0; $i < count($news); $i++) {
    echo $news[$i];
    echo $blog[$i];
}

That way you can mix them up. I will also maybe check the length of both and set the for loop for the longest. And before echoing it check if there is any post left in the array.

Well hope this helps you. Good luck.

Upvotes: 1

Related Questions