2 回答

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
在Restaurant模型中編寫關(guān)系代碼。看到你上面的問題,我明白這種關(guān)系是一對多的關(guān)系。在這種情況下,請?jiān)诓蛷d模型中寫下這個(gè)。
餐廳.php
public function dishes(){
return $this->hasMany(Dish::class);
//it define the relation type between Restaurant and Dish model
}
刀片文件
<h1> {{$restaurant->name}} </h1>
<ul>
@foreach($restaurant->dishes as $dish)
<li>{{ $dish->name }}</li>
@endforeach
</ul>
$restaurant->dishes將返回與餐廳相關(guān)的所有相關(guān)菜肴的數(shù)組/集合。用于@foreach顯示所有菜肴。
用戶自己的Html,我用ul li的例子。

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
您應(yīng)該嘗試使用 laravel 關(guān)系。喜歡
創(chuàng)建Restaurants和Dishes建模。
在您的餐廳模型中:
class Restaurants extends Model
{
function dishes() {
return $this->hasMany('App\Dishes');
}
}
在您的菜肴模型中
class Dishes extends Model
{
function restaurants() {
return $this->hasMany('App\Restaurants');
}
}
- 2 回答
- 0 關(guān)注
- 151 瀏覽
添加回答
舉報(bào)