/** * @param array $terms * @param $taxonomy * @param int $parent * @param int $order */ public static function createTaxonomies(array $terms, $taxonomy, $parent = 0, $order = 0) { if (count($terms) === 0) { return; } // only keep terms with existing entries in terms table $terms = Term::whereIn('name', $terms)->pluck('name')->all(); // create taxonomy entries for given terms foreach ($terms as $term) { Taxonomy::firstOrCreate(['taxonomy' => $taxonomy, 'term_id' => Term::where('name', $term)->first()->id, 'parent' => $parent, 'sort' => $order]); } }
/** * @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); }); }