【发布时间】:2017-08-17 10:26:05
【问题描述】:
我无法从具有hasMany、belongsToMany 等关系的两个表中进行选择。
我有桌子items
id
title
和表review
id
item_id
这是在我的项目模型中
public function review()
{
return $this->hasMany('App\Review', 'item_id','id');
}
在我的评论模型中
public function item()
{
return $this->belongsToMany('App\Item', 'item_id','id');
}
控制器
public function index()
{
$reviews = Review::with('item')->get();
return view('index', compact('reviews'));
}
在视图中,我想显示评论表中的所有 reviews 和项目表中的 titles。
@foreach($reviews as $review)
{!!$review->item()->title!!}
@endforeach
错误
SQLSTATE[42S02]:未找到基表或视图:1146 表“ps.item_id”不存在(SQL:选择
items.*、item_id.id为pivot_id、@987654337 @.item_idaspivot_item_idfromitems内连接item_idonitems.id=item_id.item_idwhereitem_id.@987654347,@ in (5,17, 18、19、20、21、22、23、24、25))
显然我的关系是错误的。有谁能帮帮我吗?
【问题讨论】:
-
您在
Review模型中的belongsToMany关系应该是belongsTo关系。