的圖我有2表作爲食品:檢索值,並把它們在與Laravel
- Food_id(PK)
- Foodname
- 說明
- 圖片
- 價格
Ans其他表是餐廳:
- RES_ID(PK)
- ResName
- 位置
我做透視表food_restaurant爲:
- ID
- Food_id(FK)
- RES_ID(FK)
我想在我的view.For食品的細節,我做功能模型Food.php作爲
public function restaurant()
{
return $this->belongsToMany('App\Restaurant','food_restaurant','Food_id','Res_id');
}
一同顯示餐廳的位置
與其他模型Restaurant.php作爲
public function food()
{
return $this->belongsToMany('App\Food','food_restaurant','Res_id','Food_id');
}
我控制器是:
class Detailscontroller extends Controller
{
public function index()
{
$Foods= Food::all();
return view('welcome', compact('Foods'));
}
public function show($Food_id)
{
$food =Food::findOrFail($Food_id);
return view('show', compact('food'));
}
}
和路線是:
Route::get('/','[email protected]');
Route::get('/{Food_id}', '[email protected]');
查看welcome.blade.php是:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
@foreach($Foods as $Food)
<p><img src="{{ asset('uploads/'.$Food->Image) }}" /><p>
<Food>
<h2>
<a href="/{{$Food->Food_id}}">{{$Food->FoodName}}
<i class="fa fa-chevron-circle-right"></i></a
</h2>
</Food>
@endforeach
</div>
</div>
@endsection
而且show.blade.php是:
@extends('layouts.app')
@section('content')
<h1 align="center">{{$food -> FoodName}}</h1>
<p><img src="{{ asset('uploads/'.$food->Image) }}" /><p>
<detail>
<h3><p>FoodDescription:</h3><h4>{{$food->Description}}</h4></p>
<h3><p>Price:</h3><h4>{{$food->Price}}</h4></p>
@foreach ($food->restaurant as $restaurant)
<h3><p>Location:</h3><h4>{{$restaurant->Location}}</p></h4>
@endforeach
@endsection
但它不顯示位置給我。如何顯示我的show.blade.php視圖中的位置?我是laravel的新手。問題在哪裏?
試着改變你的表演方法,在你的控制器,以這樣的:'''$ =食品食品::與( '餐廳') - > findOrFail($ Food_id);''' – Landjea
否,不工作。給錯誤「調用未定義的方法Illuminate \ Database \ Query \ Builder :: restaur ant()」。 – MANI
是否通過在您的視圖中使用dd($ food-> restaurant)來檢查您是否獲取了餐館關係 – Parithiban