【发布时间】:2017-11-07 04:05:00
【问题描述】:
我有两个型号Tour.php 和TourCategory.php:
Tour.php
protected $table = `tours`;
public function category()
{
return $this->belongsTo('App\TourCategory');
}
TourCategory.php
protected $table = 'tcategories';
public function tours()
{
return $this->hasMany('App\Tour');
}
我的数据库表如下:
tours表
id|title|category_id|content|
tcatgegories表
id|name
我希望使用以下代码显示属于某个类别的所有游览:
@foreach ($category->tours as $tour)
<tr>
<th>{{ $tour->id}}</th>
<td>{{ $tour->title}}</td>
<td>
<span class="label label-default">{{$category->name}}</span>
</td>
</tr>
@endforeach
使用上面的代码,我收到以下错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tours.tour_category_id' in
'where clause' (SQL: select * from `tours` where `tours`.`tour_category_id` = 1 and
`tours`.`tour_category_id` is not null) (View: F:\multiauth_tutorial-master\resources\
views\admin\categories\show.blade.php
我之前的项目也使用了相同的代码,但没有任何错误。还是我错过了什么?
【问题讨论】:
标签: php mysql laravel blade laravel-5.4