I have my table showing the browser with my information and have it iterating over 17 randomly generated items. However, It iterates over them 17 times in separate chunks. I wish to know how to tell the computer I need each unique item and not 17 of the same thing in chunks.
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{config('app.name', 'Inventory')}}</title>
</head>
<body>
<h1>Inventory Table</h1>
<p>This is the inventory table made using PHP Laravel.</p>
<ul>
@foreach($inventories as $inventory)
<li>{{$inventory['id']}} {{$inventory['title']}} {{$inventory['description']}}
{{$inventory['price']}} {{$inventory['in_stock']}} {{$inventory['on_sale']}}</li>
@endforeach
</ul>
<table>
@foreach($inventories as $inventory)
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Price</th>
<th>In stock</th>
<th>On sale</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
</tbody>
@endforeach
</table>
</body>
</html>
Here is my code. I have chosen a foreach loop because it iterates over an array, which I have. However, when I just let the first foreach statement it picked one random item generated and showed it in the browser 17 times. I have then added my second foreach loop that is directly in my table and it does bring up the 17 items but shows in the browser each item 17 times. I wish to show each individual item.
CodePudding user response:
You likely just want to adjust where you're performing the loop and what content to output. You want the @foreach
to output table
rows
for you rather than the entire table
structure repeatedly.
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Price</th>
<th>In stock</th>
<th>On sale</th>
</tr>
</thead>
<tbody>
@foreach($inventories as $inventory)
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory['title']}}</td>
<td>{{$inventory['description']}}</td>
<td>{{$inventory['price']}}</td>
<td>{{$inventory['in_stock']}}</td>
<td>{{$inventory['on_sale']}}</td>
</tr>
@endforeach
</tbody>
</table>