Use two Object Arrays to dynamically generate table values in Laravel Blade

26 Views Asked by At

I have two arrays that i want to use to populate a table, the problem is that the properties of one arrays are generated dynamically from query, the example below is the array that comes from a query that generates a table with a dynamic number of columns with names.

Array $concentrado

 ```
   0 => {#1003 ▼
+"CICLO": "23240"
+"MATRICULA": "1"
+"NOMBRE_COMPUESTO": "JOHN DOE"
+"SECTION": "B"
+"YEAR": "2"
+"CLASS": "A"
+"MATE II": "12.0"
+"QUÍMICA II": "14.0"
+"ÉTICA II": "14.0"
+"SOCIALES": "15.0"
+"LECTURA II": "16.0"
+"INGLÉS II": "16.0"
+"TIC II": "16.0"
+"ARTE II": "32.0"
+"EDU FÍS II": "0.0"
}

1 => {#1744 ▼
+"CICLO": "23240"
+"MATRICULA": "2"
+"NOMBRE_COMPUESTO": "JANE DOE"
+"SECTION": "B"
+"YEAR": "2"
+"CLASS": "A"
+"MATE II": "5.0"
+"QUÍMICA II": "11.0"
+"ÉTICA II": "12.0"
+"SOCIALES": "11.0"
+"LECTURA II": "15.0"
+"INGLÉS II": "16.0"
+"TIC II": "15.0"
+"ARTE II": "31.0"
+"EDU FÍS II": "0.0"
}
...

The second array is the dynamic headers from the query but isolated into their own table, note that the values of the second array match the dynamic properties of the first array.

Array $materias

  0 => {#1970 ▼
    +"ORDEN": "1"
    +"NOM_CORTO": "MATE II"
  }
  1 => {#1971 ▼
    +"ORDEN": "2"
    +"NOM_CORTO": "QUÍMICA II"
  }
  2 => {#1972 ▼
    +"ORDEN": "3"
    +"NOM_CORTO": "ÉTICA II"
  }
  3 => {#1973 ▼
    +"ORDEN": "4"
    +"NOM_CORTO": "SOCIALES"
  }
  4 => {#1974 ▼
    +"ORDEN": "5"
    +"NOM_CORTO": "LECTURA II"
  }
  5 => {#1975 ▼
    +"ORDEN": "6"
    +"NOM_CORTO": "INGLÉS II"
  }
  6 => {#1976 ▼
    +"ORDEN": "7"
    +"NOM_CORTO": "TIC II"
  }
  7 => {#1977 ▼
    +"ORDEN": "8"
    +"NOM_CORTO": "ARTE II"
  }
  8 => {#1978 ▼
    +"ORDEN": "9"
    +"NOM_CORTO": "EDU FÍS II"
  }

What i wanted to accomplish was to populated a html table with the values of the the first table using a nested foreach loop witht the second array ($materias) values as the property of the first ($concentrado). So far i managed to create the table headers using the second array of $materias and generate the students using the first, the only problem is i dont know how to access the data from the dynamic properties of the $concentrado array.

                <table>
                    <thead>
                        <tr>
                            <th>MATRICULA</th>
                            <th>ALUMNO</th>
                            @foreach ($materias as $item)
                                <th>{{$item->NOM_CORTO}}</th>
                            @endforeach
                        </tr>
                    </thead>
                    <tbody>
                    @foreach ($concentrado as $calif)
                        <tr>
                            <td>{{$calif->MATRICULA}}</td>
                            <td>{{$calif->NOMBRE_COMPUESTO}}</td>
                            @foreach ($materias as $colum)
                            <td>VALUE</td>
                            @endforeach
                        </tr>
                    @endforeach
                    </tbody>
                </table>

The previous html results in a table like this. HTML table from dynamic arrays

My first instinct was to try to access the dynamic properties using a second array values in the first one as a property, however this didn't work.

                @foreach ($concentrado as $calif)
                    <tr>
                        <td>{{$calif->MATRICULA}}</td>
                        <td>{{$calif->NOMBRE_COMPUESTO}}</td>
                        @foreach ($materias as $colum)
                        <td>{{$calif->$colum->NOM_CORTO}}</td>
                        @endforeach
                    </tr>
                @endforeach

I also tried to use another set of double brackets on the property space like this, again, this didn't work too.

<td>{{$colum->{{$colum->NOM_CORTO}}}}</td>

Is there a way that i could use the nested foreach loop with the array $materias to get the name of the properties for the parent foreach with the array $concentrado so i can get the values of the dynamic properties? Thank you for your time.

0

There are 0 best solutions below