PHP code example of aliziodev / laravel-terms

1. Go to this page and download the library: Download aliziodev/laravel-terms 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/ */

    

aliziodev / laravel-terms example snippets


return [
    'table_names' => [
        'terms'    => 'terms',
        'termables' => 'termables',
    ],

    // Supported: "numeric" (default), "uuid", "ulid"
    // Must match the primary key type of models using HasTerms.
    'morph_type' => 'numeric',

    // Built-in types. Arbitrary strings are also accepted at runtime.
    'types' => ['tag', 'category', 'brand', 'color', 'size'],

    // Swap in your own model if you need to extend Term.
    'model' => \Aliziodev\LaravelTerms\Models\Term::class,

    'slugs' => [
        'generate'            => true,
        'regenerate_on_update' => false,
    ],
];

use Aliziodev\LaravelTerms\Traits\HasTerms;

class Product extends Model
{
    use HasTerms;
}

use Aliziodev\LaravelTerms\Enums\TermType;

// Sync — replaces all existing tags with the new list
$product->syncTerms(['new-arrival', 'sale'], TermType::Tag);

// Attach — adds without removing existing terms
$product->attachTerms(['nike'], TermType::Brand);

// Detach specific terms
$product->detachTerms(['sale'], TermType::Tag);

// Detach all terms of a type
$product->detachTerms([], TermType::Tag);

// Detach everything
$product->detachTerms();

// Detach within a specific context only (other contexts remain untouched)
$product->detachTerms([], TermType::Tag, 'homepage');
$product->detachTerms(['sale'], TermType::Tag, 'homepage');

$product->syncTerms(['waterproof', 'breathable'], 'material');

// All terms attached to the model
$product->terms;

// Terms of a specific type
$product->termsOfType(TermType::Tag)->get();

// Fast boolean existence check — no collection loaded
$product->hasTerm(TermType::Brand, 'nike');    // true / false

// Find products that have a specific term
Product::whereHasTerm(TermType::Brand, 'nike')->get();

// Models that have ALL of the given terms (AND — default)
Product::whereHasTerms(TermType::Tag, ['new', 'sale'])->get();

// Models that have ANY of the given terms (OR)
Product::whereHasTerms(TermType::Tag, ['new', 'sale'], 'or')->get();

$product->attachTerms(['red'], TermType::Color, 'primary');
$product->attachTerms(['blue'], TermType::Color, 'secondary');

// Syncing within a context leaves other contexts untouched
$product->syncTerms(['yellow'], TermType::Color, 'primary');

// Filter by context on the pivot
$product->termsOfType(TermType::Color)->wherePivot('context', 'primary')->get();

use Aliziodev\LaravelTerms\Facades\Terms;

$term  = Terms::findOrCreate('Summer', TermType::Tag);
$terms = Terms::findOrCreateMany(['new', 'sale'], TermType::Tag);

Terms::attach($product, ['red'], TermType::Color);
Terms::sync($product, ['blue'], TermType::Color);
Terms::detach($product, ['red'], TermType::Color);

// Filter by type or slug
Term::query()->type('tag')->get();
Term::query()->slug('new-arrival')->first();

// Sort by order (asc by default)
Term::query()->type('tag')->ordered()->get();
Term::query()->ordered('desc')->get();

// config/terms.php
'model' => App\Models\Term::class,

namespace App\Models;

class Term extends \Aliziodev\LaravelTerms\Models\Term
{
    // Add relations, scopes, or accessors here
}
bash
php artisan terms:install
bash
php artisan vendor:publish --tag=terms-config
php artisan vendor:publish --tag=terms-migrations
php artisan migrate