繁体   English   中英

Laravel 4属于ToMany,其中条件

[英]Laravel 4 belongsToMany with where condition

我具有以下数据库结构: 在此处输入图片说明

Cards表与类型有很多关系,类型可以具有以下3种元类型之一:

  • 亚型
  • 类型

在我的Card模型中,我想使用以下方法:

  • types()->“ ... where types.metatype ='type'”
  • supertypes()->“ ......,其中types.metatype ='supertype'”
  • subtypes()->“ ...其中types.metatype ='subtype'”

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

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

  • cards_types
  • cards_supertypes
  • cards_subtypes

但这似乎不是一个好主意,因为在类型表中已经存在元类型数据,因此我宁愿不通过复制关系表来“重复”此信息。

这可能起作用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM