Laravel 4属于ToMany的条件

时间:2014-06-04 07:32:34

标签: php laravel-4 many-to-many

我有以下数据库结构: enter image description here

卡表与类型有多对多关系,类型可以有3种元类型之一:

  • 超型
  • 亚型

在我的卡片模型中,我想有以下方法:

  • types() - > " ...其中types.metatype ='键入'"
  • supertypes() - > " ...其中types.metatype ='超类型'"
  • subtypes() - > " ...其中types.metatype ='子类型'"

是否可以在类型表(而不是数据透视表)上添加where来实现此目的?

在此之前,我有3个不同的表:

  • cards_types
  • cards_supertypes
  • cards_subtypes

但这似乎不是一个好主意,因为元数据类型数据已经出现在类型表中,所以我宁愿不#34;重复"通过复制关系表来获取此信息。

1 个答案:

答案 0 :(得分:0)

这可能有效:

public function supertype() {
    return $this->belongsToMany('Card')->where('metatype','=','supertype');
}

public function subtype() {
    return $this->belongsToMany('Card')->where('metatype','=','subtype');
}

public function type() {
    return $this->belongsToMany('Card')->where('metatype','=','type');
}