Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带约束的Laravel雄辩嵌套关系轴_Php_Laravel_Eloquent_Relationship_Pivot Table - Fatal编程技术网

Php 带约束的Laravel雄辩嵌套关系轴

Php 带约束的Laravel雄辩嵌套关系轴,php,laravel,eloquent,relationship,pivot-table,Php,Laravel,Eloquent,Relationship,Pivot Table,我目前与拉威尔4号一起工作 我正在尝试使用约束检索集合,但它无法按预期工作 模型特征: public function valeur() { return $this->hasMany('Valeur','id_caracteristique','id'); } 瓦勒尔模型 public function caracteristique() { return $this->belongsTo('Caracteristique','id','id_caracte

我目前与拉威尔4号一起工作 我正在尝试使用约束检索集合,但它无法按预期工作

模型特征:

public function valeur() {

    return $this->hasMany('Valeur','id_caracteristique','id');

}
瓦勒尔模型

public function caracteristique() {

    return $this->belongsTo('Caracteristique','id','id_caracteristique');

}

public function produit() {

    return $this->belongsToMany('Produit','produit_valeur');

}
模型分类

public function produit() {

    return $this->belongsToMany('Produit','produit_categorie');
}
模型产品

public function valeur() {

    return $this->belongsToMany('Valeur','produit_valeur');
}
我想:

Caracteristique,其值为Category=x到produit

最终目标:能够像

caracteristique->valeur

在SQL中

SELECT c.id,v.id FROM caracteristique c
INNER JOIN valeur v on (v.id_caracteristique = c.id)
INNER JOIN produit_valeur pv on (pv.valeur_id = v.id)
INNER JOIN produit_categorie pc on (pc.produit_id = pv.produit_id)
GROUP by c.id
当我使用“加入”时,雄辩的关系不再可用

我试过这个:

$carac = Caracteristique::with(array('valeur.produit.categorie' => function($q) {

$q->whereCategorieId(2);

 }))->get();
但是这种限制没有得到尊重

有什么想法吗

问候,

找到这个糟糕的解决方案

$values = Valeur::whereHas('produit',function($q) {
$q->whereHas('categorie',function($q) {
    $q->where('categorie.id','=',2);

});
})->lists('id');

$carac = Caracteristique::with(array('valeur' =>function ($q) use($values) {
$q->wherein('id',$values);

}))->get();

拥有最佳实践的人?

如果您想要限制
特征

$catId = 2;

Caracteristique::whereHas('valeur', function ($q) use ($catId) {
  $q->whereHas('produit', function ($q) use ($catId) {
    $q->whereHas('categorie', function ($q) use ($catId) {
      $q->where('cateogorie.id', $catId);
    });
  });
})->get();
或者,如果您只想加载所有
Caracteristique
和限制相关
Valeur

Caracteristique::with(['valeur' => function ($q) use ($catId) {
  $q->whereHas('produit', function ($q) use ($catId) {
    $q->whereHas('categorie', function ($q) use ($catId) {
      $q->where('cateogorie.id', $catId);
    });
  });
}])->get();

正如您所看到的,这不是最好的代码,所以只需使用联接即可。

Hi,thx for reply,非常惊讶laravel没有为嵌套约束提供最佳解决方案,查询太糟糕了!!查询本身并没有那么糟糕,但您需要执行查询的代码却很糟糕。有我的公关,使它更容易,但泰勒还没有合并它,但我已经使用拉威尔原生关系建立了一个基本的,产品,类别,属性系统。你认为这样做是错误的吗?雄辩对于基本的东西和快速的设置来说是很好的。然而,当应用程序需要复杂的逻辑时,我会使用存储库和自定义方法,最后,如果它需要进一步的改进,通常会使用条令替换雄辩,这有点不同。话虽如此,我不会说这是错误的方法,但将来您可能会发现它不足以满足您的需要。好吧,我明白了,给出的第一个查询没有使用categorieID进行过滤,返回caracteristique/valeur的所有数据非常奇怪。。。