亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 6 - 在 Blade 中顯示名稱、郵政編碼而不是 ID

Laravel 6 - 在 Blade 中顯示名稱、郵政編碼而不是 ID

PHP
白板的微信 2022-08-19 11:02:06
我是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結果:我期望的結果:我相信有一種比為每個項目創建一個視圖函數更好的方法。當我閱讀它時,我想通過模型,可以連接城市,拉鏈和街道,就像屬于To和hasMany一樣。任何人都可以幫我嗎?
查看完整描述

1 回答

?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

您可以使用一對多關系來執行此操作。

首先,更新您的表格以正確使用雄辯關系orders

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

| id | orderno | city_id | zip_id | street_id |

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

|  1 | 100001  | 1       | 2      | 3         |

|  2 | 100002  | 1       | 1      | 2         |

|  3 | 100003  | 1       | 1      | 1         |

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

1. 定義城市和 zip 表之間的關系:


將其添加到表遷移中:zip


$table->foreign('city_id')->references('id')->on('city')->onDelete('cascade');

然后,在模型類中定義方法:city()Zip


public function city()

{

    return $this->belongsTo('App\City');

}

2. 定義 zip 和街道表之間的關系:


將其添加到表遷移中:street


$table->foreign('zip_id')->references('id')->on('zip')->onDelete('cascade');

然后,在模型類中定義方法:zip()Street


public function zip()

{

    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');

然后,為模型類中的每個關系定義一個方法:Order


public function city()

{

    return $this->belongsTo('App\City');

}


public function zip()

{

    return $this->belongsTo('App\Zip');

}


public function street()

{

    return $this->belongsTo('App\Street');

}

4. 現在在視圖(邊欄選項卡)中使用它們:


@foreach($orders as $order)

    <tr>

        <td>{{ $order->id }}</td>

        <td>{{ $order->orderno }}</td>

        <td>{{ $order->city['name'] }}</td>

        <td>{{ $order->zip['code'] }}</td>

        <td>{{ $order->street['name'] }}</td>

    </tr>

@endforeach

注意:默認情況下,表名在Laravel Eloquent中是復數形式。如果要使表名保持單數,請不要忘記在模型中設置該屬性。例如,在模型類中:$tableCity


protected $table = 'city';


查看完整回答
反對 回復 2022-08-19
  • 1 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號