1. Go to this page and download the library: Download humweb/taggables 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/ */
humweb / taggables example snippets
use Humweb\Taggables\Traits\HasTags;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasTags;
}
// Create a global tag
$post->tag('laravel'); // Available to all users
// Create a user-specific tag
$post->tagAsUser('personal-project', $user);
// Mix global and user tags
$post->tag(['php', 'laravel']); // Global
$post->tagAsUser(['favorite', 'todo'], $user); // User-specific
// Get only user tags
$userTags = $post->userTags($user->id);
// Get only global tags
$globalTags = $post->globalTags();
// Get all tags (user + global)
$allTags = $post->tags; // Returns both by default
// Check if the model has a specific tag (checks user + global by default)
if ($post->hasTag('laravel', $user->id)) {
// ...
}
// Check for global tag only
if ($post->hasGlobalTag('laravel')) {
// ...
}
// Check for user tag only
if ($post->hasUserTag('favorite', $user)) {
// ...
}
// Check if the model has any of the given tags
if ($post->hasAnyTag(['laravel', 'php'], $user->id)) {
// ...
}
// Check if the model has all of the given tags
if ($post->hasAllTags(['laravel', 'php'], $user->id)) {
// ...
}
// Get all posts with any of the given tags (], null, $userId)->get();
// Get posts with user-specific tags only
$posts = Post::withUserTags(['favorite', 'todo'], $user)->get();
// Get posts with global tags only
$posts = Post::withGlobalTags(['laravel', 'php'])->get();
// Get all posts with all of the given tags
$posts = Post::withAllTags(['laravel', 'php'], null, $userId)->get();
// Get all posts without the given tags
$posts = Post::withoutTags(['draft', 'archived'], null, $userId)->get();
// Simple single tag query
$posts = Post::taggedWith('laravel', $userId)->get();
// Tag with type
$post->tag(['important', 'urgent'], 'priority');
$post->tagAsUser(['personal', 'work'], $user, 'category');
// Get tags of a specific type
$priorityTags = $post->tagsWithType('priority');
$userCategories = $post->tagsWithType('category', $user->id);
// Query by tags with type
$posts = Post::withAnyTags(['important', 'urgent'], 'priority', $userId)->get();
use Humweb\Taggables\Models\Tag;
// Find or create a tag
$tag = Tag::findOrCreate('laravel'); // Global tag
$tag = Tag::findOrCreateForUser('personal', $user); // User tag
$tag = Tag::findOrCreateGlobal('php'); // Explicitly global
// Find or create with type
$tag = Tag::findOrCreate('important', 'priority', $user->id);
// Find or create multiple tags
$tags = Tag::findOrCreateMany(['laravel', 'php', 'mysql'], null, $user->id);
// Search tags
$tags = Tag::containing('lara')->get();
// Get tags by user
$userTags = Tag::forUser($user->id)->get();
$globalTags = Tag::global()->get();
$mixedTags = Tag::forUserWithGlobal($user->id)->get();
// Get tags by type
$priorityTags = Tag::withType('priority')->get();
// Get popular tags
$popularTags = Tag::popularTags(10, $user->id); // Mixed popular tags
$userPopular = Tag::popularUserTags(10, $user->id); // User's popular tags
$globalPopular = Tag::popularGlobalTags(10); // Global popular tags
// Get tag cloud (tags with usage weight)
$tagCloud = Tag::tagCloud($user->id); // Mixed cloud
$globalCloud = Tag::tagCloud(); // Global only
// Get unused tags
$unusedTags = Tag::unusedTags($user->id)->get();
// Check tag ownership
$tag = Tag::find(1);
if ($tag->isGlobal()) {
// This is a global tag
} elseif ($tag->isOwnedBy($user)) {
// This is the user's tag
}
// Get tag suggestions based on partial input
$suggestions = Tag::suggestTags('lara', $user->id); // Returns user + global tags
$suggestions = Tag::suggestTags('lara'); // Returns only global tags
// Get related tags (tags often used together)
$tag = Tag::findOrCreate('laravel');
$relatedTags = $tag->relatedTags($user->id);
use Humweb\Taggables\Events\TagAttached;
// In your EventServiceProvider
protected $listen = [
TagAttached::class => [
SendTagNotification::class,
],
];
namespace App\Models;
use Humweb\Taggables\Models\Tag as BaseTag;
class Tag extends BaseTag
{
// Add your custom methods
public function isPublic(): bool
{
return $this->isGlobal() || $this->metadata['public'] ?? false;
}
}
// Eager load all tags
$posts = Post::with('tags')->get();
// Eager load only user tags
$posts = Post::with(['tags' => function ($query) use ($userId) {
$query->where('user_id', $userId);
}])->get();
// Eager load only global tags
$posts = Post::with(['tags' => function ($query) {
$query->whereNull('user_id');
}])->get();
// Eager load tags with specific type
$posts = Post::with(['tags' => function ($query) {
$query->where('type', 'technology');
}])->get();
bash
php artisan tags:cleanup
# Clean up only user tags
php artisan tags:cleanup --user=123
# Clean up only global tags
php artisan tags:cleanup --global
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.