1. Go to this page and download the library: Download rinvex/laravel-categories 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/ */
rinvex / laravel-categories example snippets
// Get all categories
$allCategories = app('rinvex.categories.category')->all();
// Get instance of your model
$post = new \App\Models\Post::find(123);
// Get attached categories collection
$post->categories;
// Get attached categories query builder
$post->categories();
$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;
if ($category->save()) {
$moved = $category->hasMoved();
}
app('rinvex.categories.category')->create($attributes); // Saved as root
$category = app('rinvex.categories.category')->fill($attributes);
$category->save(); // Saved as root
// #1 Implicit save
$category->saveAsRoot();
// #2 Explicit save
$category->makeRoot()->save();
// #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);
// #1 Using deferred insert
$category->prependToNode($parent)->save();
// #2 Using parent category
$parent->prependNode($category);
# Explicit save
$category->afterNode($neighbor)->save();
$category->beforeNode($neighbor)->save();
# Implicit save
$category->insertAfterNode($neighbor);
$category->insertBeforeNode($neighbor);
// #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);
// #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);
// 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();
// 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();
// 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();
// 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();
// Check if category is a descendant of other category
$bool = $category->isDescendantOf($parent);
// Check whether the category is a root:
$bool = $category->isRoot();
// Other checks
$category->isChildOf($other);
$category->isAncestorOf($other);
$category->isSiblingOf($other);
// Check if tree is broken
$bool = app('rinvex.categories.category')->isBroken();
// Get tree error statistics
$data = app('rinvex.categories.category')->countErrors();
app('rinvex.categories.category')->fixTree();
shell
php artisan rinvex:publish:categories
shell
php artisan rinvex:migrate:categories
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.