/** * @param $query * @param $term * @param $taxonomy * @return mixed */ public function scopeWithTerm($query, $term, $taxonomy) { $term_ids = Taxonomy::where('taxonomy', $taxonomy)->pluck('term_id'); $term = Term::whereIn('id', $term_ids)->where('name', '=', $term)->first(); $taxonomy = Taxonomy::where('term_id', $term->id)->first(); return $query->whereHas('taxonomies', function ($q) use($term, $taxonomy) { $q->where('term_id', $term->id); }); }