cyberfly
5/11/2017 - 1:35 AM

Using whereHas on eloquent scope http://stackoverflow.com/questions/14621943/laravel-how-to-use-where-conditions-for-relations-column

<?php

class RestaurantsController{

  public function index(){
    
    //after using scope
     
    Restaurant::hasWifi()->hasCuisine(6)->get();
    
  }
}
<?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();
    
  }
}
<?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);
        });
    }
}