Last active
November 28, 2017 06:37
-
-
Save cyberfly/81fbc8bf2eabdc9565b0a5606a9bb609 to your computer and use it in GitHub Desktop.
Using whereHas on eloquent scope http://stackoverflow.com/questions/14621943/laravel-how-to-use-where-conditions-for-relations-column
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 | |
| class RestaurantsController{ | |
| public function index(){ | |
| //after using scope | |
| Restaurant::hasWifi()->hasCuisine(6)->get(); | |
| } | |
| } |
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 | |
| class Restaurant extends Eloquent | |
| { | |
| // Relations here | |
| public function scopeHasWifi($query) | |
| { | |
| return $query->whereHas('facilities', function($query) { | |
| return $query->where('wifi', true); | |
| }); | |
| } | |
| public function scopeHasCuisine($query, $cuisineId) | |
| { | |
| return $query->whereHas('cuisines', function($query) use ($cuisineId) { | |
| return $query->where('id', $cuisineId); | |
| }); | |
| } | |
| } |
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 | |
| class RestaurantsController{ | |
| public function index(){ | |
| //before using scope | |
| Restaurant::whereHas('facilities', function($query) { | |
| return $query->where('wifi', true); | |
| })->get(); | |
| Restaurant::whereHas('cuisines', function($query) use ($cuisineId) { | |
| return $query->where('id', $cuisineId); | |
| })->get(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment