我花了两天时间尝试解决这个问题,但没有成功。我有三个表,类别、项目和相关项目。每个项目都属于一个类别,一个类别可以有许多项目,这部分工作正常,现在问题出在相关项目上。我在相关项目表中有3个字段,id(仅自增),ritemf_id、riteml_id,它们都指向项目表中的item_id。
我的目标是显示带有详细信息和相关项目的项目,也就是说,如果item1有很多相关项,如item2、item3、item4等,则需要像这样显示: item_title: item1 related item: item2
item3
item4 controller
我的目标是显示带有详细信息和相关项目的项目,也就是说,如果item1有很多相关项,如item2、item3、item4等,则需要像这样显示: item_title: item1 related item: item2
item3
item4 controller
$items = Item::orderBy('category_id', 'asc')->with('category')->get()->groupBy('category_id');
$categories = Category::orderBy('category_id', 'asc')->get();
return view('home',['items' => $items,'ritems' => $ritems,'categories' => $categories]);
项目模态框
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
public function relateditems()
{
return $this->belongsTo('App\Relateditem', 'ritemf_id');
}
相关物品模态框:
class Relateditem extends Model
{
protected $table="relateditems";
protected $fillable=['ritemf_id','riteml_id'];
protected $primaryKey='id';
public $timestamps=false;
public function items()
{
return $this->belongsTo('App\Item', 'item_id');
}
}
在Blade中显示带有其类别的项目(正常工作)
@if (!empty($categoryItems->first()->category))
{{ $categoryItems->first()->category->category_name }} @else {{$category_id}} @endif
@foreach($categoryItems as $item)
{{$item->item_title}}
${{$item->item_price}}
@endforeach
@endforeach
ritemf_id
和riteml_id
之间有什么区别?它们的用途是什么? - Val Cajes Luminarias