Laravel Eloquent 多表 where 条件查询

3
我花了两天时间尝试解决这个问题,但没有成功。我有三个表,类别、项目和相关项目。每个项目都属于一个类别,一个类别可以有许多项目,这部分工作正常,现在问题出在相关项目上。我在相关项目表中有3个字段,id(仅自增),ritemf_id、riteml_id,它们都指向项目表中的item_id。
我的目标是显示带有详细信息和相关项目的项目,也就是说,如果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

enter image description here


ritemf_idriteml_id之间有什么区别?它们的用途是什么? - Val Cajes Luminarias
2个回答

1

relateditems()Item模型中的定义对我来说不正确,Item可能有多个相关项,那么这应该是hasMany/beongsToMany关联。

假设它是hasMany,然后更新您的项目模型如下:

public function relateditems()  {
    return $this->hasMany('App\Relateditem', 'ritemf_id');       
}

并且为每个项目急切地加载其相关项。保留HTML标签,不进行解释。
$items = Item::orderBy('category_id', 'asc')
             ->with(['category', 'relateditems'])
             ->get()
             ->groupBy('category_id');

我认为groupBy方法从集合类中使用,用于对检索到的数据进行分组,而不是在数据库端进行分组。

我尝试使用您的解决方案,但对我没有起作用。因此,如果我像显示项目和类别一样使用相同的方式,我会使用以下代码:$ritems = Relateditem::orderBy('ritemf_id', 'asc')->with('items')->get()->groupBy('ritemf_id'); 在修改模型后,它只显示相关项目的数据,您知道每个项目都有一个循环,其中包含相关项目。 - user9155494

0
你需要修复 Item 模型和 RelatedItem 模型中的两个关系。relateditems 应该是 hasMany,因为一个物品可以有多个相关物品。
你还需要使用 riteml_id 键在 RelatedItem 中定义 belongsTo 关系到 Item。

Item 模型

public function category() 
{
    return $this->belongsTo('App\Category', 'category_id');
}

public function relateditems() 
{
    return $this->hasMany('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 item() //i have change it to item instead items, because belongsTo always return single record
    {  
        return $this->belongsTo('App\Item', 'riteml_id');
    }
 }

获取数据

$items = Item::orderBy('category_id', 'asc')->with('category','relateditems', 'relateditems.item')->get()->groupBy('category_id');

foreach($items as $categoryId => $groupItems){
   echo $groupItems->first()->category;
   foreach($groupItems as $item) {
       echo $item->item_tile;
       ...
       foreach($item->relateditems as $relatedItem){
           if ($relatedItem->item){
              echo $relatedItem->item->item_tile; //this will show you related item title
           }
       }
    }
}

针对单个项目

   $item = Item::with('category','relateditems', 'relateditems.item')->find(1);

   foreach($item->relateditems as $relatedItem){
       if ($relatedItem->item){
          echo $relatedItem->item->item_tile; //this will show you related item title
       }
   }

relatedItems表只有riteml_id与item表相关,从中可以获取相关项目列表。relateditems只会提供相关项目的ID,而不是引用的“Item”。 - rkj
relateditems有两个引用:ritemf_id和riteml_id。 - user9155494
是的,你用什么引用它们?请检查我添加了一个关系的“Relateditem”模型,代码如下:return $this->belongsTo('App\Item', 'riteml_id'); - rkj
这个设计可以,你能告诉我 ritemf_idriteml_id 存储的是什么吗?因为我给出的答案是基于查看你的代码后的假设。 - rkj
让我们在聊天中继续这个讨论 - rkj
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接