Laravel whereHas on multiple relationships

HowApped

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

edi9999

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel whereHas on Many-to-Many relationships

From Dev

Laravel multiple whereHas criteria for relationship

From Dev

Laravel multiple where condition in whereHas callback

From Dev

Laravel With Wherehas

From Dev

Eager load multiple relationships in Laravel

From Dev

multiple one to one relationships laravel

From Dev

How to return multiple relationships with Laravel Eloquent?

From Dev

Laravel, simplifying relationships: multiple sortable models

From Dev

Laravel: How to use multiple pivot table relationships

From Dev

Laravel Eloquent: eager loading of multiple nested relationships

From Dev

Multiple relationships on same table with two column Laravel

From Dev

Laravel: How to use multiple pivot table relationships

From Dev

How to return multiple relationships with Laravel Eloquent?

From Dev

Laravel combining has() and whereHas()

From Dev

Laravel WhereHas with polymorphic relations

From Dev

Laravel - is there a way to combine whereHas and with

From Dev

Merge 'with' and 'whereHas' in Laravel 5

From Dev

Laravel WhereHas with polymorphic relations

From Dev

Laravel upgrade issue with whereHas

From Dev

Laravel Search Relations whereHas

From Dev

Laravel Eloquent nulls in whereHas

From Dev

Laravel - Multiple query function to multiple eager loaded relationships

From Dev

Relationships and with in Laravel?

From Dev

Laravel Relationships (With)

From Dev

Column not found - Laravel whereHas error

From Dev

Laravel passing variable to wherehas query

From Dev

Laravel: Difference between where and whereHas

From Dev

Laravel 5 whereHas chaining and orWhere

From Dev

Laravel 5.1 Eloquent with() and max() using multiple has-many relationships

Related Related

HotTag

Archive