Skip to content
Advertisement

How can I populate two columns in a table with different foreach iterations in php/laravel?

Hey everyone I’m struggling with a problem to display some data. I have two arrays of teams called rosterWhite and rosterBlack

On my view I have a table with two columns “White” and “Black”.

To illustrate, these are my teams int he Controller:

JavaScript

And this is my table in the view:

JavaScript

As you can see, the current implementation will go through the array creating a list of the nicknames inside a single cell and using line jumps.

JavaScript

What I’m trying to figure out is how can I iterate through arrays, but instead of filling in everything in a single cell, that each name is a row.

something like this:

JavaScript

Iterating outside of the TD, creates the list horizontally. So each name in a single cell but all names from both teams in the same row.

I can’t find a way to iterate through white list, and add cells under White column, then iterate through black list and add names under Black column.

Any tips for this? Maybe I should create two different tables that are somehow connected? thanks!

Advertisement

Answer

You could try something like this:

In your controller:

JavaScript

In your view:

JavaScript

And if you want to replace the @if condition checking array key existence, you could easily do this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement