I have 2 table as Food:
- Food_id(pk)
- Foodname
- Description
- Image
- Price
Ans other table is Restaurant:
- Res_id(pk)
- ResName
- Location
I make Pivot table food_restaurant as:
- id
- Food_id(fk)
- Res_id(fk)
I want to show Location of Restaurant along with food details in my view.For that I make function in model Food.php as
public function restaurant()
{
return $this->belongsToMany('App\Restaurant','food_restaurant','Food_id','Res_id');
}
And other model Restaurant.php as
public function food()
{
return $this->belongsToMany('App\Food','food_restaurant','Res_id','Food_id');
}
My Controller is:
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'));
}
}
And Routes are:
Route::get('/','DetailsController@index');
Route::get('/{Food_id}', 'DetailsController@show');
View welcome.blade.php is:
@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
And show.blade.php is:
@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
But it does not show Location to me.How can I show Location in my show.blade.php view?I am new to laravel.Where is the problem?
return the food array to the view with associated restaurant.