【问题标题】:laravel eloquent multiple tables where criterialaravel 雄辩的多个表 where 条件
【发布时间】:2018-12-09 19:50:01
【问题描述】:

我花了两天时间试图解决这个问题,但没有运气。 我有 3 个表、类别、项目和相关项目,每个项目都在一个类别下,类别可以有很多项目,这部分工作正常,现在问题出在相关项目中,我在表 realteditems 中有 3 个字段,id(只是autoincrement), ritemf_id,riteml_id 指的是 items 表中的 item_id。 我想要做的是显示项目及其详细信息和与该项目相关的项目,这意味着如果 item1 有很多 realted 项目,例如 item2,item3,item4 .. 所以需要这样显示

item_title: item1

相关项目:

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

相关项目模式:

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

在刀片中显示项目及其类别(工作正常)

 @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_idriteml_id 有什么区别。有什么用?

标签: php laravel


【解决方案1】:

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

假设它的hasMany 然后将您的项目模型更新为

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

并急切地为每个项目加载您的相关项目

$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');在修改模态但它只显示来自相关项目的数据后,你知道每个项目都有一个循环,这个循环包含相关项目
【解决方案2】:

您需要修复Item 模型和RelatedItem 模型中的两个关系。 relateditems 应该是hasMany,因为一个项目可以有许多相关项目。 您还需要使用 riteml_id 键在 RelatedItem 中定义 belongsTo 与 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.item”是什么意思?
  • relatedItems 表只有 riteml_id 引用项目表,从中您将获得相关项目列表。 relateditems 只会给你相关的项目ID而不是引用的Item
  • relateditems 有两个引用 ritemf_id 和 riteml_id
  • 是的,你用什么来引用它们?检查Relateditem模型我添加了一个关系return $this->belongsTo('App\Item', 'riteml_id');
  • 好吧,问题是我在表项目中有项目,同时这些项目中的一些项目可能有相关项目,而不是所有项目,所以我想我可以制作另一个表来显示相关项目,这就是为什么这个表有两个字段,因为可能item1有相关的2,3,4,你有什么其他的想法来设计相关的表吗?
猜你喜欢
  • 2018-09-18
  • 2020-09-28
  • 2021-12-18
  • 2017-11-05
  • 2021-02-13
  • 2014-08-15
  • 2014-11-09
  • 1970-01-01
  • 2015-08-08
相关资源
最近更新 更多