Created
May 2, 2017 01:26
-
-
Save cyberfly/4d20b4c20dea54d8b0a3d59ed06afc71 to your computer and use it in GitHub Desktop.
Eloquent whereHas and whereOr inside 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| public function search() | |
| { | |
| $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' ); | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment