在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):rinvex/laravel-categories开源软件地址(OpenSource Url):https://github.com/rinvex/laravel-categories开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Rinvex CategoriesRinvex Categories is a polymorphic Laravel package, for category management. You can categorize any eloquent model with ease, and utilize the power of Nested Sets, and the awesomeness of Sluggable, and Translatable models out of the box. Installation
UsageTo add categories support to your eloquent models simply use Manage your categoriesYour categories are just normal eloquent models, so you can deal with it like so. Nothing special here!
Manage your categorizable modelThe API is intutive and very straightfarwad, so let's give it a quick look: // Get all categories
$allCategories = app('rinvex.categories.category')->first();
// Get instance of your model
$post = new \App\Models\Post::find();
// Get attached categories collection
$post->categories;
// Get attached categories query builder
$post->categories(); You can attach categories in various ways: // Single category id
$post->attachCategories(1);
// Multiple category IDs array
$post->attachCategories([1, 2, 5]);
// Multiple category IDs collection
$post->attachCategories(collect([1, 2, 5]));
// Single category model instance
$categoryInstance = app('rinvex.categories.category')->first();
$post->attachCategories($categoryInstance);
// Single category slug
$post->attachCategories('test-category');
// Multiple category slugs array
$post->attachCategories(['first-category', 'second-category']);
// Multiple category slugs collection
$post->attachCategories(collect(['first-category', 'second-category']));
// Multiple category model instances
$categoryInstances = app('rinvex.categories.category')->whereIn('id', [1, 2, 5])->get();
$post->attachCategories($categoryInstances);
And as you may have expected, you can check if categories attached: // Single category id
$post->hasAnyCategories(1);
// Multiple category IDs array
$post->hasAnyCategories([1, 2, 5]);
// Multiple category IDs collection
$post->hasAnyCategories(collect([1, 2, 5]));
// Single category model instance
$categoryInstance = app('rinvex.categories.category')->first();
$post->hasAnyCategories($categoryInstance);
// Single category slug
$post->hasAnyCategories('test-category');
// Multiple category slugs array
$post->hasAnyCategories(['first-category', 'second-category']);
// Multiple category slugs collection
$post->hasAnyCategories(collect(['first-category', 'second-category']));
// Multiple category model instances
$categoryInstances = app('rinvex.categories.category')->whereIn('id', [1, 2, 5])->get();
$post->hasAnyCategories($categoryInstances);
Advanced usageGenerate category slugsRinvex Categories auto generates slugs and auto detect and insert default translation for you if not provided, but you still can pass it explicitly through normal eloquent app('rinvex.categories.category')->create(['name' => ['en' => 'My New Category'], 'slug' => 'custom-category-slug']);
Smart parameter detectionRinvex Categories methods that accept list of categories are smart enough to handle almost all kinds of inputs as you've seen in the above examples. It will check input type and behave accordingly. Retrieve all models attached to the categoryYou may encounter a situation where you need to get all models attached to certain category, you do so with ease as follows: $category = app('rinvex.categories.category')->find(1);
$category->entries(\App\Models\Post::class)->get(); Query scopesYes, Rinvex Categories shipped with few awesome query scopes for your convenience, usage example: // Single category id
$post->withAnyCategories(1)->get();
// Multiple category IDs array
$post->withAnyCategories([1, 2, 5])->get();
// Multiple category IDs collection
$post->withAnyCategories(collect([1, 2, 5]))->get();
// Single category model instance
$categoryInstance = app('rinvex.categories.category')->first();
$post->withAnyCategories($categoryInstance)->get();
// Single category slug
$post->withAnyCategories('test-category')->get();
// Multiple category slugs array
$post->withAnyCategories(['first-category', 'second-category'])->get();
// Multiple category slugs collection
$post->withAnyCategories(collect(['first-category', 'second-category']))->get();
// Multiple category model instances
$categoryInstances = app('rinvex.categories.category')->whereIn('id', [1, 2, 5])->get();
$post->withAnyCategories($categoryInstances)->get();
Category translationsManage category translations with ease as follows: $category = app('rinvex.categories.category')->find(1);
// Update title translations
$category->setTranslation('name', 'en', 'New English Category Title')->save();
// Alternatively you can use default eloquent update
$category->update([
'name' => [
'en' => 'New Category',
'ar' => 'تصنيف جديد',
],
]);
// Get single category translation
$category->getTranslation('name', 'en');
// Get all category translations
$category->getTranslations('name');
// Get category title in default locale
$category->name;
Manage your nodes/nestedsetsInserting categoriesMoving and inserting categories includes several database queries, so transaction is automatically started when category is saved. It is safe to use global transaction if you work with several models. Another important note is that structural manipulations are deferred until you hit If model is successfully saved it doesn't mean that category was moved. If your application
depends on whether the category has actually changed its position, use if ($category->save()) {
$moved = $category->hasMoved();
} Creating categoriesWhen you simply create a category, it will be appended to the end of the tree: app('rinvex.categories.category')->create($attributes); // Saved as root
$category = app('rinvex.categories.category')->fill($attributes);
$category->save(); // Saved as root In this case the category is considered a root which means that it doesn't have a parent. Making a root from existing categoryThe category will be appended to the end of the tree: // #1 Implicit save
$category->saveAsRoot();
// #2 Explicit save
$category->makeRoot()->save(); Appending and prepending to the specified parentIf you want to make category a child of other category, you can make it last or first child.
Suppose that // #1 Using deferred insert
$category->appendToNode($parent)->save();
// #2 Using parent category
$parent->appendNode($category);
// #3 Using parent's children relationship
$parent->children()->create($attributes);
// #5 Using category's parent relationship
$category->parent()->associate($parent)->save();
// #6 Using the parent attribute
$category->parent_id = $parent->getKey();
$category->save();
// #7 Using static method
app('rinvex.categories.category')->create($attributes, $parent); And only a couple ways to prepend: // #1 Using deferred insert
$category->prependToNode($parent)->save();
// #2 Using parent category
$parent->prependNode($category); Inserting before or after specified categoryYou can make # Explicit save
$category->afterNode($neighbor)->save();
$category->beforeNode($neighbor)->save();
# Implicit save
$category->insertAfterNode($neighbor);
$category->insertBeforeNode($neighbor); Building a tree from arrayWhen using static method $category = app('rinvex.categories.category')->create([
'name' => [
'en' => 'New Category Title',
],
'children' => [
[
'name' => 'Bar',
'children' => [
[ 'name' => 'Baz' ],
],
],
],
]);
Rebuilding a tree from arrayYou can easily rebuild a tree. This is useful for mass-changing the structure of the tree.
Given the $data = [
[ 'id' => 1, 'name' => 'foo', 'children' => [ ... ] ],
[ 'name' => 'bar' ],
];
app('rinvex.categories.category')->rebuildTree($data, $delete); There is an id specified for category with the title of Category
Retrieving categoriesIn some cases we will use an AncestorsAncestors make a chain of parents to the category. Helpful for displaying breadcrumbs to the current category. // #1 Using accessor
$result = $category->getAncestors();
// #2 Using a query
$result = $category->ancestors()->get();
// #3 Getting ancestors by primary key
$result = app('rinvex.categories.category')->ancestorsOf($id); DescendantsDescendants are all categories in a sub tree, i.e. children of category, children of children, etc. // #1 Using relationship
$result = $category->descendants;
// #2 Using a query
$result = $category->descendants()->get();
// #3 Getting descendants by primary key
$result = app('rinvex.categories.category')->descendantsOf($id);
// #3 Get descendants and the category by id
$result = app('rinvex.categories.category')->descendantsAndSelf($id); Descendants can be eagerly loaded: $categories = app('rinvex.categories.category')->with('descendants')->whereIn('id', $idList)->get(); SiblingsSiblings are categories that have same parent. $result = $category->getSiblings();
$result = $category->siblings()->get(); To get only next siblings: // Get a sibling that is immediately after the category
$result = $category->getNextSibling();
// Get all siblings that are after the category
$result = $category->getNextSiblings();
// Get all siblings using a query
$result = $category->nextSiblings()->get(); To get previous siblings: // Get a sibling that is immediately before the category
$result = $category->getPrevSibling();
// Get all siblings that are before the category
$result = $category->getPrevSiblings();
// Get all siblings using a query
$result = $category->prevSiblings()->get(); Getting related models from other tableImagine that each category // Get ids of descendants
$categories = $category->descendants()->pluck('id');
// Include the id of category itself
$categories[] = $category->getKey();
// Get products
$goods = Product::whereIn('category_id', $categories)->get(); Now imagine that each category // Get ids of descendants
$categories = $category->descendants()->pluck('id');
// Include the id of category itself
$categories[] = $category->getKey();
// Get posts
$posts = \App\Models\Post::withCategories($categories)->get(); Including category depthIf you need to know at which level the category is: $result = app('rinvex.categories.category')->withDepth()->find($id);
$depth = $result->depth; Root category will be at level 0. Children of root categories will have a level of 1, etc. To get categories of s |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论