Skip to content

Instantly share code, notes, and snippets.

@cyberfly
Last active November 28, 2017 06:37
Show Gist options
  • Select an option

  • Save cyberfly/81fbc8bf2eabdc9565b0a5606a9bb609 to your computer and use it in GitHub Desktop.

Select an option

Save cyberfly/81fbc8bf2eabdc9565b0a5606a9bb609 to your computer and use it in GitHub Desktop.
<?php
class RestaurantsController{
public function index(){
//after using scope
Restaurant::hasWifi()->hasCuisine(6)->get();
}
}
<?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);
});
}
}
<?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