WhereHas()/或Where不会按预期约束查询 [英] WhereHas() / orWhereHas not constraining the query as expected

查看:226
本文介绍了WhereHas()/或Where不会按预期约束查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的whereHas方法在Laravel 4.1中为渴望加载的关系添加约束.

I am using the new whereHas method to add a constraint on an eager loaded relationship in Laravel 4.1.

$broadcast = $broadcast->whereHas('season', function( $query ) use ( $parameterValues ){
              $query->where('name', '2011-12' );
          });
      }

在此示例中,我正在搜索足球/足球广播,其关系是广播belongsTo一个赛季

In this example, I'm searching for a football/soccer broadcast where the relationship is that a broadcast belongsTo a Season

我想为团队(俱乐部)添加一个约束,其中关系是广播hasOne homeClub和广播hasOne awayClub.我想要球队(利物浦)是主场还是客场俱乐部的结果,所以我尝试使用orWhereHas-

I want to add a constraint for team (club) where the relationship is that a Broadcast hasOne homeClub and a broadcast hasOne awayClub. I want results for where a team (liverpool) is either the home or the away club so I try to use the orWhereHas - a featured added in L4.1

        $broadcast = $broadcast->with('homeClub')->whereHas('homeClub', function( $query ) use ( $parameterValues ){
              $query->where('abb', $parameterValues['club_abbs'] );
          });
          $broadcast = $broadcast->with('awayClub')->orWhereHas('awayClub', function( $query ) use ( $parameterValues ){
              $query->where('abb', $parameterValues['club_abbs'] );
          });

但是这似乎消除了我第一个示例中提到的赛季限制.就像通过链接orWhereHas一样,我的where方法已经忘记了"它们约束的内容.

But this just seems to cancel out the constraint on the season that's mentioned in my first example. It's like, by chaining the the orWhereHas, my where methods have 'forgotten' what they were constraining.

任何帮助,非常感谢-谢谢

Any help, greatly appreciated - thanks

乔恩

推荐答案

我不正确地使用了该方法.我先加载了两个关联,然后链接了whereHasorWhereHas

I was using the method incorrectly. I loaded both associations first and then chained the whereHas and orWhereHas

$broadcast = $broadcast->with('homeClub');
$broadcast = $broadcast->with('awayClub');


    $broadcast = $broadcast->whereHas('homeClub', function( $query ) use ( $parameterValues ){
      $query->where('abb', 'liverpool' );
  })->orWhereHas('awayClub', function( $query ) use ( $parameterValues ){
      $query->where('abb', 'liverpool' );
  });

这篇关于WhereHas()/或Where不会按预期约束查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆