Laravel whereHas 有多个关系 [英] Laravel whereHas on multiple relationships

查看:13
本文介绍了Laravel whereHas 有多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道这个新功能是否可以在多个关系上执行?p>

例如,我有一个查询,我想过滤的不仅仅是俱乐部名称(相关问题),还有地区名称.

在本例中,我想要查询结果,其中俱乐部(俱乐部关系)名称为阿森纳,地区为澳大利亚(领土关系)

$ret->with('territory')->with('homeClub')->with('awayClub');$ret->whereHas('领土',function($query){$query->where('region','Australia');})->whereHas('homeClub', function ($query) {$query->where('name', 'Arsenal' );})->orWhereHas('awayClub', function ($query) {$query->where('name', 'Arsenal' );});

当执行这个查询时 - 结果并没有限制 whereHas 只是俱乐部之一.

可以链接 whereHas 以过滤先前关系的 whereHas 的结果吗?如果没有,有什么建议吗?

谢谢

乔恩

解决方案

是的,这是可能的.

生成的 SQL 可能是:

SELECT * FROM ... WHERE (领土约束) AND (homeClub 约束) OR (awayClub 约束)

这意味着如果满足 awayClub 约束,则将检索该行.我想您想在生成的 sql 中添加一个括号:

SELECT * FROM ... WHERE (领土约束) AND ((homeClub constratint) OR (awayClub 约束))

为此,您需要将两个查询都嵌套在 where:

$ret->with('territory')->with('homeClub')->with('awayClub');$ret->whereHas('领土',function($query){$query->where('region','Australia');})->哪里(函数($subQuery){$subQuery->whereHas('homeClub', function ($query) {$query->where('name', 'Arsenal' );})->orWhereHas('awayClub', function ($query) {$query->where('name', 'Arsenal' );});});

Does anyone know if this new feature can be performed on multiple relationships?

For example, I have a query where I want to filter on not only the club name (related question) but also the territory name.

In this example, I'd like query results where the club (club relationship) name is Arsenal and the the region is Australia (territory relationship)

$ret->with('territory')->with('homeClub')->with('awayClub');
    $ret->whereHas('territory',function( $query ){
            $query->where('region','Australia');
        })->whereHas('homeClub', function ( $query ) {
            $query->where('name', 'Arsenal' );
        })->orWhereHas('awayClub', function ( $query ) {
            $query->where('name', 'Arsenal' );
        });

When executing this query - the result isn't constraining the territory whereHas just the clubs one.

Can whereHas be chained to filter the results on previous relationship's whereHas? Any suggestions if not?

thanks

jon

解决方案

Yes that's possible.

The generated SQL will probably be:

SELECT * FROM ... WHERE (territory constraint) AND (homeClub constratint) OR (awayClub constraint)

This means that if awayClub constraint is satisfied, the line will be retrieved. I think you want to add a parenthesis to the generated sql:

SELECT * FROM ... WHERE (territory constraint) AND ((homeClub constratint) OR (awayClub constraint))

to do that, you need to nest both queries inside a where:

$ret->with('territory')->with('homeClub')->with('awayClub');
    $ret->whereHas('territory',function( $query ){
        $query->where('region','Australia');
    })
    ->where(function($subQuery)
    {   
        $subQuery->whereHas('homeClub', function ( $query ) {
            $query->where('name', 'Arsenal' );
        })
        ->orWhereHas('awayClub', function ( $query ) {
            $query->where('name', 'Arsenal' );
        });
    });

这篇关于Laravel whereHas 有多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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