【问题标题】:Laravel, many-to-many relationship among multiple modelsLaravel,多个模型之间的多对多关系
【发布时间】:2019-09-01 14:31:14
【问题描述】:

我有多个具有多对多关系的模型

这是模型

  • 新闻栏目
  • 类别
  • 子类别
  • 已获批准的新闻
  • 待定新闻

每个新闻版块可以有多个类别

每个类别可以有多个子类别

每个子类别可以有多个已批准新闻待处理新闻

我想要新闻,其中包含类别子类别待处理/批准新闻

诸如此类的东西

类别,带有子类别批准新闻

我尝试使用数据透视表但无法获得结果

型号如下

新闻栏目

class NewsSection extends Model
{
     public function categories()
    {
    return $this->belongsToMany(Category::class);
    }
}

类别

class Category extends Model
{
    public function subcats(){
       return $this->belongsToMany(SubCategory::class);
    }
    public function newssections(){
      return $this->belongsToMany(NewsSection::class);
    }
}

子类别

class SubCategory extends Model
{
    public function category(){
     return $this->belongsTo(Category::class);
    }

    public function approvednews(){
     return $this->belongsToMany(ApprovedNews::class);
    }

   public function pendingnews(){
     return $this->belongsToMany(PendingNews::class);
   }

}

已获批准的新闻

class ApprovedNews extends Model
{

    public function subcategories (){
     return $this->belongsToMany(SubCategory::class);
   }
}

待定新闻

class PendingdNewsextends Model
{

    public function subcategories (){
     return $this->belongsToMany(SubCategory::class);
    }
}

更新 这是我到目前为止所做的

$news =Category::with('subcats.approvednews')->where('id',1)->get();

我得到了所有带有子类别和类别的已批准新闻

如果我这样做,我如何修改它以获取每个类别的特定子目录和批准的新闻

$news =Category::with('subcats.approvednews')->where('subcats.id',1)->get();

我收到类似 id ambiguous 的错误

是否可以从关系中挑选项目,例如为 的每个 subcat 只返回 2 个 subcats3 个已批准的新闻 >所选类别

获取每个subcat类别已批准新闻待定新闻计数

提前致谢

【问题讨论】:

  • 你遇到的实际问题是什么?
  • 当我尝试获得任何类别的批准新闻时,我得到的所有项目都返回我这样做 $items=Category::with('subcategory')->where('id',1)- >get();相反,它没有获得类别或主要部分
  • 你能用一个具体的代码示例来更新这个问题吗?你正在尝试做什么以及你得到了什么?
  • @Rao 当你在做$items=Category::with('subcategory')->where('id',1)->get(); 时,你是想获取 id 为 1 的类别,还是子类别为 1 的类别?
  • 请看更新

标签: laravel eloquent pivot many-to-many relation


【解决方案1】:

错误“error like id ambiguous”表示您需要在where('id', 1)中指定表,如where('table.id', 1),以便MySQL知道您指的是哪个表中的id列。

你可以这样constrain the models returned by with

Category::with(['subcats' => function(Builder $query) {
    $query->where('id', '=', 1);
}]);

你也可以count relations:

$subcat = SubCategory::withCount(['approvednews']);
$subcat->approvednews_count;

限制急切加载的关系是不可能的per the docs

一种解决方法可能是从ApprovedNews 开始:

ApprovedNews::whereHas(['subcategories' => function(Builder $query) {
    $query->where('id', '=', 1);
}])->limit(10);

【讨论】:

    【解决方案2】:

    我有一些关于如何让它发挥作用的建议。在您的 cmets 中,您说您在执行以下操作时遇到了问题:

    $items=Category::with('subcategory')->where('id',1)->get();

    'subcategory' 来自哪里?从模型的外观来看,类别和子类别之间的关系称为subcats。因此,您需要将其更改为:

    $items=Category::with('subcats')->where('id',1)->get();

    如果您将其转储出来,您应该会看到您将获得 ID 为 1 的类别,以及加载的子类别。测试您的关系是否正常工作的方法如下:

    $category = Category::find(1);
    $subCats = $category->subcats()->get();
    dd($subCats);
    

    在你的人际关系中,我建议不要使用SubCategory::class,而是尝试使用return $this->belongsToMany('App\SubCategory');,这样模型肯定是相互关联的。

    一旦您测试了彼此之间的关系有效,您就可以开始测试您可以从 a->b->c 等进行的测试。

    【讨论】:

      【解决方案3】:

      可能正在使用“嵌套急切加载”和“范围”,您可以执行类似的操作

      $pendings = NewSection::with('categories.subCategories')->pending()->get()
      $approved = NewSection::with('categories.subCategories')->approved()->get()
      

      没有测试过,但可以试试,可能稍作修改,就可以达到目标。

      如果你想返回一个集合,你可以合并它

      $approved->merge($pendings);
      

      但是,你应该避免它。

      【讨论】:

        猜你喜欢
        • 2021-05-16
        • 2020-03-27
        • 2018-05-23
        • 1970-01-01
        • 2015-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多