Php laravel雄辩的多个表,其中标准

Php laravel雄辩的多个表,其中标准,php,laravel,Php,Laravel,我花了两天时间试图解决这个问题,但运气不好。 我有3个表、类别、项目和相关项目,每个项目在一个类别下,类别可以有多个项目,这部分工作正常,现在问题在相关项目中,我在表realteditems中有3个字段,id(仅自动递增)、ritemf_id、riteml_id,它们指的是项目表中的项目id。 我想做的是显示项目及其详细信息和与此项目相关的项目,这意味着如果项目1有许多实际项目,例如项目2、项目3、项目4。。所以我们需要像这样展示 项目标题:item1 相关项目: class Relatedi

我花了两天时间试图解决这个问题,但运气不好。 我有3个表、类别、项目和相关项目,每个项目在一个类别下,类别可以有多个项目,这部分工作正常,现在问题在相关项目中,我在表realteditems中有3个字段,id(仅自动递增)、ritemf_id、riteml_id,它们指的是项目表中的项目id。 我想做的是显示项目及其详细信息和与此项目相关的项目,这意味着如果项目1有许多实际项目,例如项目2、项目3、项目4。。所以我们需要像这样展示

项目标题:item1

相关项目:

 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');
 }
 }
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');
    }
 }
项目2

项目3

项目4

控制器

   $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');       
  }
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 items() 
 {  
    return $this->belongsTo('App\Item', 'item_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');
    }
 }
在blade中显示其类别的项目(工作正常)

relateditems()
定义在
项目
模型中对我来说似乎不正确,项目可能有多个相关项目,那么这应该是
具有多个/beongomanty
关联

假设它的
有许多
,然后将您的项目模型更新为

public function relateditems()  {
    return $this->hasMany('App\Relateditem', 'ritemf_id');       
}
并为每个项目加载相关项目

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

我假设该方法用于从集合类到组检索到的数据(不在数据库端)

您需要修复
模型和
相关项
模型中的关系。相关项应该是
hasMany
,因为一个项可以有许多相关项。 您还需要使用
riteml\u id
键定义相关项与项之间的
belongsTo
关系

项目模式

 public function category() 
  {
    return $this->belongsTo('App\Category', 'category_id');
   }
 public function relateditems() 
 {
 return $this->belongsTo('App\Relateditem', 'ritemf_id');       
  }
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 items() 
 {  
    return $this->belongsTo('App\Item', 'item_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
       }
   }

我试图使用您的解决方案,但它对我不起作用,因此,如果我使用与我相同的方式来显示项目和类别,我将使用$ritems=Relateditem::orderBy('ritemf_id','asc')->with('items')->get()->groupBy('ritemf_id');修改模式但仅显示相关项的数据后,您知道每个项都有一个循环。此循环包含“获取数据”部分中的相关项。您所说的“relateditems.item”是什么意思?relateditems表只有引用项表的
riteml\u id
,您将从中获得相关项列表。relateditems只会给您提供相关的items id,而不是引用的
Item
relateditems有两个引用ritemf\u id和riteml\u id,您用什么引用它们?检查
Relateditem
model我已经添加了一个关系
return$this->belongsTo('App\Item','riteml\u id')好的问题是我在表项中有项,同时可能这些项中的某些项有相关项,而不是所有项,所以我想我可以制作另一个表来显示相关项,这就是为什么这些表有两个字段,因为可能项1有相关的2,3,4,您对设计相关表格有什么其他想法吗?
ritemf\u id
riteml\u id
之间有什么区别。有什么用?