Search code examples
phparrayslaravellaravel-7

How to pass array value to show in table views laravel


I'm trying to make table in view that has value from array in my controller.

Here's my controller:

public function Loan Data(Request $request)
{
  $a = array("1", "2", "3", "4", "5");
  $b = array("110", "120", "130", "140", "150");
  $c = array("200", "220", "230", "240", "250");

  $array_all = ['bunga'=>$a,'pokok'=>$b,'pinjaman'=>$c,];
return view('show',[
        'all' => $array_all
    ]);
}

how can i pass the array to views that has table like this

<table>
<tr>
    <th>Data 1</th>
    <th>Data 2</th>
    <th>Data 3</th>
</tr>
<tr>
    <td>Value of array a here</td>
    <td> value of array b here</td>
    <td> value of array c here</td>
</tr>
</table>

Solution

  • If I understood correctly, you want something like this:

    
    <table>
        <tr>
            <th>Row Title</th>
            
            <th>Data 1</th>
            <th>Data 2</th>
            <th>Data 3</th>
        </tr>
        @foreach ($all as $rowTitle => $row)
        <tr>
            <td>{{ $rowTitle }}</td>
            
            @foreach ($row as $col)
                 <td>{{ $col }}</td>
            @endforeach
        </tr>
        @endforeach
    </table>
    

    If you want the data to rotate diagonally, then try something like this:

    <table>
        <tr>
            <th>Data 1</th>
            <th>Data 2</th>
            <th>Data 3</th>
        </tr>
        @foreach ($all['bunga'] as $index => $value)
        <tr>
            <td>{{ $all['bunga'][$index] }}</td>
            <td>{{ $all['pokok'][$index] }}</td>
            <td>{{ $all['pinjaman'][$index] }}</td>
        </tr>
        @endforeach
    </table>