I'm a beginner in Laravel, I need to show a name instead of an ID in Laravel blade.
These are the tables in the database:
city:
+----+--------+
| id | name |
+----+--------+
| 1 | Vienna |
| 2 | Linz |
+----+--------+
zip:
+----+---------+------+-------------+
| id | city_id | code | name |
+----+---------+------+-------------+
| 1 | 1 | 1010 | 1. district |
| 2 | 1 | 1020 | 2. district |
| 3 | 1 | 1030 | 3. district |
| 4 | 2 | 4020 | Linz |
+----+---------+------+-------------+
street:
+----+--------+---------------+
| id | zip_id | name |
+----+--------+---------------+
| 1 | 1 | Burgring |
| 2 | 1 | Seilergasse |
| 3 | 2 | Praterstrasse |
+----+--------+---------------+
orders:
+----+---------+------+-----+--------+
| id | orderno | city | zip | street |
+----+---------+------+-----+--------+
| 1 | 100001 | 1 | 2 | 3 |
| 2 | 100002 | 1 | 1 | 2 |
| 3 | 100003 | 1 | 1 | 1 |
+----+---------+------+-----+--------+
Controller:
$orders = Order::all();
return view('orders-show', compact('orders'));
Blade:
@foreach($orders as $order)
<tr>
<td>{{$order->id}}</td>
<td>{{$order->orderno}}</td>
<td>{{$order->city}}</td>
<td>{{$order->zip}}</td>
<td>{{$order->street}}</td>
</tr>
@endforeach
I believe there is a much better way than to create a view function for each item. As I read it, I suppose that through Model, can connect city, zip and street, something like belongsTo and hasMany.
Can anyone help me?