从2个不同的表中检索值,并将它们放在Laravel的视图中

时间:2016-08-15 12:29:40

标签: php laravel eloquent laravel-5.2

我有2个表作为食物

  • Food_id(PK)
  • Foodname
  • 描述
  • 图像
  • 价格

Ans其他表是餐厅

  • RES_ID(PK)
  • ResName
  • 位置

我将Pivot表 food_restaurant 设为:

  • ID
  • Food_id(FK)
  • RES_ID(FK)

我想在我的视图中显示餐厅的位置以及食物详情。为此我在模型 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('/','DetailsController@index');

Route::get('/{Food_id}', 'DetailsController@show');

查看 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的新手。问题出在哪里?

0 个答案:

没有答案