models/Post.php
public function scopeFilterCategories($query, $categories)
    {
        return $query->whereHas('categories', function($q) use ($categories) {
            $q->whereIn('id', $categories);
        });
    }
public function scopeFilterCategories($query, $categories)
    {
        return $query->whereHas('categories', function($q) use ($categories) {
            $q->whereIn('id', $categories);
        });
    }
public function scopeFilterCategories ( $ query , $ categories ) { return $ query -> whereHas ( 'categories' , function ( $ q ) use ( $ categories ) { $ q -> whereIn ( 'id' , $ categories ) ; } ) ; }
Allows filtering for specifc categories.
@param  Illuminate\Query\Builder  $query      QueryBuilder
@param  array                     $categories List of category ids
@return Illuminate\Query\Builder              QueryBuilder
Allows filtering for specifc categories .