Как создать динамический c ряд строк в таблице в laravel - PullRequest
0 голосов
/ 29 января 2020

Я новичок ie в laravel, я пытаюсь показать таблицу, как на рисунке ниже

https://i.stack.imgur.com/l52Vo.jpg

@foreach($report_data as $index => $item)
<tbody class="table-content">
    <tr>

        <td>{{ $index+1 }}</td>
        <td style="text-align:left;">{{ $item->customer_fullName }}</td>
        <td>{{ $item->customer_phone }}</td>
        <td>{{ $item->customer_detail_licensePlate }}</td>
        <td>{{ $item->vehicle_brand_name }}</td>
        <td>{{ $item->vehicle_model_name }}</td>

    </tr>
</tbody>
@endforeach

, но вывод похож на изображение ниже https://i.stack.imgur.com/rSktI.png

Спасибо !!

Ответы [ 3 ]

0 голосов
/ 29 января 2020
<table class="table">
 <thead class="thead-dark">
  <tr>
    <th scope="col">#</th>
    <th scope="col">Customer</th>
    <th scope="col">Licence Number</th>
    <th scope="col">Brand</th>
    <th scope="col">Brand Type</th>
  </tr>
</thead>
<tbody>
  @foreach($report_data as $index => $item)
  <tbody class="table-content">
  <tr>
    <td>{{ $index+1 }}</td>
    <td style="text-align:left;">{{ $item->customer_fullName }}</td>
    <td>{{ $item->customer_phone }}</td>
    <td>{{ $item->customer_detail_licensePlate }}</td>
    <td>{{ $item->vehicle_brand_name }}</td>
    <td>{{ $item->vehicle_model_name }}</td>
   </tr>
 </tbody>
  @endforeach
</tbody>

используйте этот CDN для Bootstrap стиля

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
0 голосов
/ 29 января 2020

Я бы сделал это так:

В контроллере выберите клиентов, которые хотят загрузить автомобили.

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

В представлении:

<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 голосов
/ 29 января 2020

Вам не нужно повторять tbody каждый раз, просто итерируйте <tr></tr>, как показано ниже

<table>
    <thead>
        <tr>
            <th>No</th>
            <th>Customer</th>
            <th>Phone</th>
            <th>License Number</th>
            <th>Brand</th>
            <th>Brand Type</th>
        </tr>
    </thead>
    <tbody class="table-content">
        @foreach($report_data as $index => $item)
        <tr>
            <td>{{ $index+1 }}</td>
            <td rowspan="2" style="text-align:left;">{{ $item->customer_fullName }}</td> //rowspan should be in some condition
            <td>{{ $item->customer_phone }}</td>
            <td>{{ $item->customer_detail_licensePlate }}</td>
            <td>{{ $item->vehicle_brand_name }}</td>
            <td>{{ $item->vehicle_model_name }}</td>
        </tr>
        @endforeach
    </tbody>
</table>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...