Laravel 6 - 在 Blade 中显示名称、邮政编码而不是 ID

我是Laravel的初学者,我需要在Laravel刀片中显示一个名字而不是ID。


这些是数据库中的表:


城市:


+----+--------+

| id | name   |

+----+--------+

|  1 | Vienna |

|  2 | Linz   |

+----+--------+


邮编:


+----+---------+------+-------------+

| id | city_id | code | name        |

+----+---------+------+-------------+

|  1 |       1 | 1010 | 1. district |

|  2 |       1 | 1020 | 2. district |

|  3 |       1 | 1030 | 3. district |

|  4 |       2 | 4020 | Linz        |

+----+---------+------+-------------+

街:


+----+--------+---------------+

| id | zip_id | name          |

+----+--------+---------------+

|  1 |      1 | Burgring      |

|  2 |      1 | Seilergasse   |

|  3 |      2 | Praterstrasse |

+----+--------+---------------+

订单:


+----+---------+------+-----+--------+

| id | orderno | city | zip | street |

+----+---------+------+-----+--------+

|  1 | 100001  | 1    | 2   | 3      |

|  2 | 100002  | 1    | 1   | 2      |

|  3 | 100003  | 1    | 1   | 1      |

+----+---------+------+-----+--------+

控制器:


$orders = Order::all();

return view('orders-show', compact('orders'));

叶片:


@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

结果:

http://img4.mukewang.com/62fefd440001a72e08040211.jpg

我期望的结果:

http://img4.mukewang.com/62fefd530001182307990218.jpg

我相信有一种比为每个项目创建一个视图函数更好的方法。当我阅读它时,我想通过模型,可以连接城市,拉链和街道,就像属于TohasMany一样。

任何人都可以帮我吗?


白板的微信
浏览 125回答 1
1回答

呼啦一阵风

您可以使用一对多关系来执行此操作。首先,更新您的表格以正确使用雄辩关系:orders+----+---------+---------+--------+-----------+| id | orderno | city_id | zip_id | street_id |+----+---------+---------+--------+-----------+|&nbsp; 1 | 100001&nbsp; | 1&nbsp; &nbsp; &nbsp; &nbsp;| 2&nbsp; &nbsp; &nbsp; | 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||&nbsp; 2 | 100002&nbsp; | 1&nbsp; &nbsp; &nbsp; &nbsp;| 1&nbsp; &nbsp; &nbsp; | 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||&nbsp; 3 | 100003&nbsp; | 1&nbsp; &nbsp; &nbsp; &nbsp;| 1&nbsp; &nbsp; &nbsp; | 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|+----+---------+---------+--------+-----------+1. 定义城市和 zip 表之间的关系:将其添加到表迁移中:zip$table->foreign('city_id')->references('id')->on('city')->onDelete('cascade');然后,在模型类中定义方法:city()Zippublic function city(){&nbsp; &nbsp; return $this->belongsTo('App\City');}2. 定义 zip 和街道表之间的关系:将其添加到表迁移中:street$table->foreign('zip_id')->references('id')->on('zip')->onDelete('cascade');然后,在模型类中定义方法:zip()Streetpublic function zip(){&nbsp; &nbsp; return $this->belongsTo('App\Zip');}3. 定义城市、邮政编码、街道和订单表之间的关系:将以下行添加到表迁移中:orders$table->foreign('city_id')->references('id')->on('city');$table->foreign('zip_id')->references('id')->on('zip');$table->foreign('street_id')->references('id')->on('street');然后,为模型类中的每个关系定义一个方法:Orderpublic function city(){&nbsp; &nbsp; return $this->belongsTo('App\City');}public function zip(){&nbsp; &nbsp; return $this->belongsTo('App\Zip');}public function street(){&nbsp; &nbsp; return $this->belongsTo('App\Street');}4. 现在在视图(边栏选项卡)中使用它们:@foreach($orders as $order)&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $order->id }}</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $order->orderno }}</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $order->city['name'] }}</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $order->zip['code'] }}</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>{{ $order->street['name'] }}</td>&nbsp; &nbsp; </tr>@endforeach注意:默认情况下,表名在Laravel Eloquent中是复数形式。如果要使表名保持单数,请不要忘记在模型中设置该属性。例如,在模型类中:$tableCityprotected $table = 'city';
打开App,查看更多内容
随时随地看视频慕课网APP