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


// config/terms.php
return [
    'types' => [
        'category',
        'tag',
        'size',
        'color',
        'unit',
        'type',
        'brand',
        'model',
        'variant',
    ],
];

use Aliziodev\LaravelTerms\Traits\HasTerms;

class Post extends Model
{
    use HasTerms;
}

// Create term
$term = Term::create([
    'name' => 'Electronics',
    'type' => 'category'
]);

// Attach terms
$post->attachTerms(['electronics', 'gadgets']);

// Sync terms
$post->syncTerms(['electronics', 'gadgets']);

// Detach terms
$post->detachTerms(['electronics']);

// Get terms by type
$categories = $post->getTermsByType('category');

// Get terms grouped
$terms = $post->getTermsByGroups(['category', 'tag']);

// Get tree structure
Term::tree();
Term::treeFlat();

// Get ancestors & descendants
$term->getAncestors();
$term->getDescendants();

// Get computed attributes
$term->path;      // 'electronics/phones/iphone'
$term->depth;     // 2
$term->is_leaf;   // true/false
$term->is_root;   // true/false

// Move term
$term->moveToStart();
$term->moveToEnd();
$term->moveBefore($otherTerm);
$term->moveAfter($otherTerm);
$term->moveToOrder(5);

// Set meta
$term->setMeta(['icon' => 'phone']);

// Get meta
$term->getMeta('icon');

// Update meta
$term->updateMeta(['icon' => 'new-phone']);

// Remove meta
$term->removeMeta('icon');

// Update meta for type
Term::updateMetaForType('category', ['visible' => true]);

Term::search('keyword');    // Search in name, slug, description
Term::type('category');     // Filter by type
Term::root();              // Get root terms
Term::leaf();              // Get leaf terms