[Solved] How to create dynamic rowspan in table in laravel


I would do it as so:

In the controller, select the customers eager loading the vehicles.

$customers = Customer::with('vehicles')->get();
return view('customers')->with('customers', $customers);

In the view:

<table>
    @foreach($customers as $index => $customer)
        <tr>
            <td rowspan="$customer->vehicles->count()">
               {{ $index+1 }}
            </td>
            <td rowspan="$customer->vehicles->count()">
               {{ $customer->fullName }}
            </td>
            <td rowspan="$customer->vehicles->count()">
               {{ $customer->phone }}
            </td>
            <td> {{$customer->vehicles[0]->licensePlate }} </td>
            <td> {{$customer->vehicles[0]->brandName }} </td>
            <td> {{$customer->vehicles[0]->modelName }} </td>
        </tr>
        @for($i=1;$i<$customer->vehicles->count())
        <tr>
            <td> {{$customer->vehicles[$i]->licensePlate }} </td>
            <td> {{$customer->vehicles[$i]->brandName }} </td>
            <td> {{$customer->vehicles[$i]->modelName }} </td>
        </tr>
        @endfor
    @endforeach
</table>

0

solved How to create dynamic rowspan in table in laravel