Php 从两个不同的表中检索值,并将它们放在具有Laravel的视图中
我有两张桌子作为食物:Php 从两个不同的表中检索值,并将它们放在具有Laravel的视图中,php,laravel,eloquent,laravel-5.2,Php,Laravel,Eloquent,Laravel 5.2,我有两张桌子作为食物: 食品标识(pk) 食品名称 描述 形象 价格 另一张桌子是餐厅: 资源id(pk) 重新命名 位置 我将Pivot table食品餐厅制作为: 身份证 食品标识(fk) 资源id(fk) 我想在视图中显示餐厅的位置以及食物的详细信息。为此,我在modelfood.php中创建函数 public function restaurant() { return $this->belongsToMany('App\Restaurant','food_re
- 食品标识(pk)
- 食品名称
- 描述
- 形象
- 价格
- 资源id(pk)
- 重新命名
- 位置
- 身份证
- 食品标识(fk)
- 资源id(fk)
public function restaurant()
{
return $this->belongsToMany('App\Restaurant','food_restaurant','Food_id','Res_id');
}
public function food()
{
return $this->belongsToMany('App\Food','food_restaurant','Res_id','Food_id');
}
和其他模型Restaurant.php一样
public function restaurant()
{
return $this->belongsToMany('App\Restaurant','food_restaurant','Food_id','Res_id');
}
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'));
}
}
@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
@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
路线包括:
Route::get('/','DetailsController@index');
Route::get('/{Food_id}', 'DetailsController@show');
视图welcome.blade.php是:
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'));
}
}
@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
@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
@extends('layouts.app'))
@节(“内容”)
@foreach($Foods as$Food)
图像)}}”/>
{{$Food->FoodName}
FoodName}}
图像)}}”/>
食品说明:{{$food->Description}
价格:{{$food->Price}
@foreach($food->restaurant as$restaurant)
地点:{{$restaurant->Location}
@endforeach
@端部
但它不会向我显示位置。我如何在show.blade.php视图中显示位置?我是laravel的新手。问题出在哪里?将食物数组返回到关联餐厅的视图中
public function show($Food_id)
{
$food = Food::with('restaurant')->findOrFail($Food_id);
return view('show', compact('food'));
}
将食物数组返回到具有关联餐厅的视图
public function show($Food_id)
{
$food = Food::with('restaurant')->findOrFail($Food_id);
return view('show', compact('food'));
}
尝试将控制器中的显示方法更改为:
$food=food::with('restaurant')->findOrFail($food\u id)代码>否,不起作用。调用未定义的方法Illumb\Database\Query\Builder::Restar时出现“错误”ant()”。您是否在视图中使用dd($food->restaurant)检查是否获得了餐厅关系?您是否有合适的名称空间并在模型中使用?假设您的名称空间只是“App”,您是否有use-App\Restaurant
在Food.php中定义,并使用App\Food代码>在Restaurant.php中定义?@Landjea不,我没有添加这些名称空间。现在我添加了这些名称空间,它可以工作:)Thanx很多。尝试将控制器中的显示方法更改为:$food=food::with('Restaurant')->findOrFail($food\u id)代码>否,不起作用。调用未定义的方法Illumb\Database\Query\Builder::Restar时出现“错误”ant()”。您是否在视图中使用dd($food->restaurant)检查是否获得了餐厅关系?您是否有合适的名称空间并在模型中使用?假设您的名称空间只是“App”,您是否有use-App\Restaurant
在Food.php中定义,并使用App\Food
在Restaurant.php中定义?@Landjea不,我没有添加这些名称空间。现在我添加了这些名称空间,它可以工作:)Thanx很多。不,它不工作。调用undefined方法Illumb\Database时出错\查询\Builder::restauRant()”。我想添加与食品的“类别”关系。我可以使用“$food=food::with('restaurant','Category')->findOrFail($food_id);”不,它不起作用。对未定义的方法\Database进行“错误”调用\查询\Builder::restauRant()”。我想添加与食品的“类别”关系。我可以使用“$food=food::with('restaurant','Category')->findOrFail($food_id);”