Skip to content

Commit 20cf611

Browse files
committed
feat(taxonomy): add type-specific operations and query scopes
1 parent d36bd9c commit 20cf611

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,29 @@ $product->detachTaxonomies($taxonomyIds);
331331
$product->syncTaxonomies($taxonomyIds);
332332
$product->toggleTaxonomies($taxonomyIds);
333333

334+
// Type-specific operations (NEW)
335+
$product->attachTaxonomiesOfType(TaxonomyType::Category, $categoryIds);
336+
$product->detachTaxonomiesOfType(TaxonomyType::Category, $categoryIds);
337+
$product->syncTaxonomiesOfType(TaxonomyType::Category, $categoryIds);
338+
$product->toggleTaxonomiesOfType(TaxonomyType::Tag, $tagIds);
339+
334340
// Check relationships
335341
$hasCategory = $product->hasTaxonomies($categoryIds);
336342
$hasAllTags = $product->hasAllTaxonomies($tagIds);
337343
$hasType = $product->hasTaxonomyType(TaxonomyType::Category);
338344

345+
// Type-specific relationship checks (NEW)
346+
$hasCategories = $product->hasTaxonomiesOfType(TaxonomyType::Category, $categoryIds);
347+
$hasAllCategories = $product->hasAllTaxonomiesOfType(TaxonomyType::Category, $categoryIds);
348+
339349
// Get related taxonomies
340350
$allTaxonomies = $product->taxonomies;
341351
$categories = $product->taxonomiesOfType(TaxonomyType::Category);
342352
$hierarchical = $product->getHierarchicalTaxonomies(TaxonomyType::Category);
353+
354+
// Utility methods (NEW)
355+
$categoryCount = $product->getTaxonomyCountByType(TaxonomyType::Category);
356+
$firstCategory = $product->getFirstTaxonomyOfType(TaxonomyType::Category);
343357
```
344358

345359
### Query Scopes for Filtering
@@ -354,6 +368,11 @@ $products = Product::withTaxonomyType(TaxonomyType::Category)->get();
354368
$products = Product::withAnyTaxonomies([$category1, $category2])->get();
355369
$products = Product::withAllTaxonomies([$tag1, $tag2])->get();
356370

371+
// Type-specific filtering (NEW)
372+
$products = Product::withAnyTaxonomiesOfType(TaxonomyType::Category, [$category1, $category2])->get();
373+
$products = Product::withAllTaxonomiesOfType(TaxonomyType::Tag, [$tag1, $tag2])->get();
374+
$products = Product::withoutTaxonomiesOfType(TaxonomyType::Category, [$category1])->get();
375+
357376
// Filter by taxonomy slug (any type)
358377
$products = Product::withTaxonomySlug('electronics')->get();
359378

@@ -365,6 +384,10 @@ $products = Product::withTaxonomyHierarchy($parentCategoryId)->get();
365384

366385
// Filter by depth level
367386
$products = Product::withTaxonomyAtDepth(2, TaxonomyType::Category)->get();
387+
388+
// Order by taxonomy type (NEW)
389+
$products = Product::orderByTaxonomyType(TaxonomyType::Category)->get();
390+
$products = Product::orderByTaxonomyType(TaxonomyType::Category, 'desc')->get();
368391
```
369392

370393
#### Scope Chaining vs Single Scope with Type
@@ -1375,6 +1398,129 @@ try {
13751398
}
13761399
```
13771400

1401+
## 🎯 Type-Specific Operations
1402+
1403+
The package provides specialized methods for working with taxonomies of specific types, offering more precise control over your taxonomy relationships.
1404+
1405+
### Type-Specific Attachment Methods
1406+
1407+
```php
1408+
use Aliziodev\LaravelTaxonomy\Enums\TaxonomyType;
1409+
1410+
// Attach only categories to a product
1411+
$product->attachTaxonomiesOfType(TaxonomyType::Category, [$category1->id, $category2->id]);
1412+
1413+
// Attach only tags to a product
1414+
$product->attachTaxonomiesOfType(TaxonomyType::Tag, ['featured', 'bestseller']);
1415+
1416+
// Detach specific categories
1417+
$product->detachTaxonomiesOfType(TaxonomyType::Category, [$category1->id]);
1418+
1419+
// Detach all categories (keep other types)
1420+
$product->detachTaxonomiesOfType(TaxonomyType::Category);
1421+
1422+
// Sync categories (replace all categories with new ones)
1423+
$product->syncTaxonomiesOfType(TaxonomyType::Category, [$newCategory1->id, $newCategory2->id]);
1424+
1425+
// Toggle specific tags
1426+
$product->toggleTaxonomiesOfType(TaxonomyType::Tag, [$tag1->id, $tag2->id]);
1427+
```
1428+
1429+
### Type-Specific Relationship Checks
1430+
1431+
```php
1432+
// Check if product has any of the specified categories
1433+
$hasAnyCategory = $product->hasTaxonomiesOfType(TaxonomyType::Category, [$category1->id, $category2->id]);
1434+
1435+
// Check if product has all specified tags
1436+
$hasAllTags = $product->hasAllTaxonomiesOfType(TaxonomyType::Tag, [$tag1->id, $tag2->id]);
1437+
1438+
// Get count of taxonomies by type
1439+
$categoryCount = $product->getTaxonomyCountByType(TaxonomyType::Category);
1440+
$tagCount = $product->getTaxonomyCountByType(TaxonomyType::Tag);
1441+
1442+
// Get first taxonomy of specific type
1443+
$firstCategory = $product->getFirstTaxonomyOfType(TaxonomyType::Category);
1444+
$firstTag = $product->getFirstTaxonomyOfType(TaxonomyType::Tag);
1445+
```
1446+
1447+
### Type-Specific Query Scopes
1448+
1449+
```php
1450+
// Find products with any of the specified categories
1451+
$products = Product::withAnyTaxonomiesOfType(TaxonomyType::Category, [$category1->id, $category2->id])->get();
1452+
1453+
// Find products with all specified tags
1454+
$products = Product::withAllTaxonomiesOfType(TaxonomyType::Tag, [$tag1->id, $tag2->id])->get();
1455+
1456+
// Find products without specific categories
1457+
$products = Product::withoutTaxonomiesOfType(TaxonomyType::Category, [$category1->id])->get();
1458+
1459+
// Order products by taxonomy type (products with categories first)
1460+
$products = Product::orderByTaxonomyType(TaxonomyType::Category)->get();
1461+
$products = Product::orderByTaxonomyType(TaxonomyType::Category, 'desc')->get();
1462+
```
1463+
1464+
### Practical Examples
1465+
1466+
#### E-commerce Product Management
1467+
1468+
```php
1469+
// Set up product categories and tags separately
1470+
$product = Product::create(['name' => 'Smartphone']);
1471+
1472+
// Attach categories
1473+
$product->attachTaxonomiesOfType(TaxonomyType::Category, [
1474+
$electronics->id,
1475+
$smartphones->id,
1476+
$mobile->id
1477+
]);
1478+
1479+
// Attach tags
1480+
$product->attachTaxonomiesOfType(TaxonomyType::Tag, [
1481+
$featured->id,
1482+
$bestseller->id,
1483+
$newArrival->id
1484+
]);
1485+
1486+
// Update only categories without affecting tags
1487+
$product->syncTaxonomiesOfType(TaxonomyType::Category, [
1488+
$electronics->id,
1489+
$smartphones->id,
1490+
$android->id // Replace mobile with android
1491+
]);
1492+
1493+
// Check product classification
1494+
if ($product->hasTaxonomiesOfType(TaxonomyType::Category, [$smartphones->id])) {
1495+
// Apply smartphone-specific logic
1496+
}
1497+
1498+
// Get category count for display
1499+
$categoryCount = $product->getTaxonomyCountByType(TaxonomyType::Category);
1500+
echo "This product is in {$categoryCount} categories";
1501+
```
1502+
1503+
#### Content Management
1504+
1505+
```php
1506+
// Find articles in specific categories with certain tags
1507+
$articles = Article::withAllTaxonomiesOfType(TaxonomyType::Category, [$technology->id])
1508+
->withAnyTaxonomiesOfType(TaxonomyType::Tag, [$tutorial->id, $guide->id])
1509+
->orderByTaxonomyType(TaxonomyType::Category)
1510+
->get();
1511+
1512+
// Remove outdated tags while keeping categories
1513+
$article->detachTaxonomiesOfType(TaxonomyType::Tag, [$outdated->id, $deprecated->id]);
1514+
```
1515+
1516+
### Benefits of Type-Specific Operations
1517+
1518+
1. **Precision**: Work with specific taxonomy types without affecting others
1519+
2. **Performance**: More efficient queries when dealing with specific types
1520+
3. **Maintainability**: Clearer code intent and easier debugging
1521+
4. **Flexibility**: Mix and match different taxonomy types as needed
1522+
5. **Safety**: Prevent accidental modification of unrelated taxonomy types
1523+
13781524
## Troubleshooting
13791525

13801526
### Common Issues

0 commit comments

Comments
 (0)