1. Go to this page and download the library: Download edulazaro/laraterms library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// AppServiceProvider::boot()
use EduLazaro\Laraterms\Facades\Laraterms;
Laraterms::resolveScopeUsing(fn ($model) => $model->organization ?? null);
class Post extends Model
{
use HasTerms;
public function termsScope(): ?\Illuminate\Database\Eloquent\Model
{
return $this->organization;
}
}
use EduLazaro\Laraterms\Concerns\HasTerms;
class Post extends Model { use HasTerms; }
// Attach / sync / detach
$post->attachTerm('Laravel', 'tags'); // find-or-create
$post->attachTerms(['Laravel', 'PHP'], 'tags');
$post->syncTerms(['Laravel', 'Vue'], 'tags'); // replace in the taxonomy
$post->syncTerm('Tutorial', 'categories'); // the one term of the taxonomy, null to clear
$post->detachTerm('Laravel', 'tags');
$post->detachAll('tags');
// Read
$post->terms; // all attached
$post->termsIn('tags'); // by taxonomy
$post->termIn('categories'); // the one term, or null
$post->hasTermsIn('tags'); // bool
// Query
Post::whereHasTerm('laravel', 'tags')->get();
Post::whereHasAnyTerm(['laravel', 'vue'], 'tags')->get();
Post::whereHasAllTerms(['laravel', 'tutorial'], 'tags')->get();
Post::whereInTaxonomy('categories')->get();
// Single-locale: use only `name`
Term::create(['name' => 'Laravel', 'taxonomy' => 'tags']);
$term->name; // "Laravel"
// Multi-locale
Term::create([
'name' => 'Tag', // canonical fallback
'name_translations' => ['en' => 'Tag', 'es' => 'Etiqueta'],
'taxonomy' => 'tags',
]);
$term->name; // "Etiqueta" if locale=es, "Tag" if locale=en or fallback
class Term extends \EduLazaro\Laraterms\Models\Term
{
use \Spatie\Translatable\HasTranslations;
protected $translatable = ['name_translations', 'description_translations'];
}
Term::search('impuesto')->get(); // matches even if the user is on /en
Term::where('search_text', 'like', '%laravel%')->get();
Term::whereFullText('search_text', 'laravel')->get(); // if your engine supports FULLTEXT (default migration adds it)
use EduLazaro\Laraterms\Support\TermTree;
$tree = TermTree::for('categories', $organization); // Collection of roots with children populated (1 query)
$tree = TermTree::for('categories', $organization, m, $depth]) {
echo str_repeat('. ', $depth) . $term->name . "\n";
}
$term->ancestors(); // Collection<Term> root to parent
$term->breadcrumb(' > '); // "Tech > Web > Laravel"
$term->descendantIds(); // all descendant ids
$term->deactivate(); // hide from pickers
$term->activate(); // re-activate
Term::active()->inTaxonomy('tags')->forScope($org)->get(); // only active
Term::inactive()->inTaxonomy('tags')->forScope($org)->get(); // only inactive
class Term extends \EduLazaro\Laraterms\Models\Term {
use \Illuminate\Database\Eloquent\SoftDeletes;
}
// + migration with $table->softDeletes();
$dup = Term::byHandle('laravel-framework', 'tags')->first();
$canonical = Term::byHandle('laravel', 'tags')->first();
$dup->mergeInto($canonical, deactivateSource: true);
// 1. Moves all termables from $dup to $canonical (without duplicating)
// 2. Recalculates terms_count on the canonical
// 3. Deactivates $dup (kept in DB but hidden from pickers).
// Pass deactivateSource: false for a real delete with cascade.