Download the PHP package angkor/laravel-categories without Composer

On this page you can find all versions of the php package angkor/laravel-categories. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-categories

Angkor Categories

This package is a fork of rinvex/laravel-categories. We are grateful to the Rinvex team for their excellent work in creating and maintaining the original package.

Angkor 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.

Packagist License

Key Features

Credits

This package is a fork of the excellent rinvex/laravel-categories package created by Rinvex. We are grateful for their work in creating the original foundation for this package.

Requirements

Installation

  1. Install the package via composer:

  2. Publish resources (migrations and config files):

  3. Execute migrations via the following command:

  4. Done!

Usage

Usage

To add categories support to your eloquent models simply use \Angkor\Categories\Traits\Categorizable trait:

Manage your categories

Your categories are just normal eloquent models, so you can manage them like any other model. The package comes with all the essential tools you need for category management.

Notes: Angkor Categories builds on these excellent packages:

Working with categorizable models

The API is intutive and very straightforward, so let's give it a quick look:

You can attach categories in various ways:

Notes:

  • The attachCategories() method attach the given categories to the model without touching the currently attached categories, while there's the syncCategories() method that can detach any records that's not in the given items, this method takes a second optional boolean parameter that's set detaching flag to true or false.
  • To detach model categories you can use the detachCategories() method, which uses exactly the same signature as the attachCategories() method, with additional feature of detaching all currently attached categories by passing null or nothing to that method as follows: $post->detachCategories();.

And as you may have expected, you can check if categories attached:

Notes:

  • The hasAnyCategories() method check if ANY of the given categories are attached to the model. It returns boolean true or false as a result.
  • Similarly the hasAllCategories() method uses exactly the same signature as the hasAnyCategories() method, but it behaves differently and performs a strict comparison to check if ALL of the given categories are attached.

Advanced usage

Generate category slugs

Angkor Categories auto generates slugs and auto-detects and inserts default translations for you if not provided. You can also pass them explicitly through the normal eloquent create method, as follows:

Note: Check Sluggable package for further details.

Smart parameter detection

The category methods 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 category

You may encounter a situation where you need to get all models attached to certain category, you do so with ease as follows:

Query scopes

The package ships with several useful query scopes for your convenience. Here's how to use them:

Notes:

  • The withAnyCategories() scope finds posts with ANY attached categories of the given. It returns normally a query builder, so you can chain it or call get() method for example to execute and get results.
  • Similarly there's few other scopes like withAllCategories() that finds posts with ALL attached categories of the given, withoutCategories() which finds posts without ANY attached categories of the given, and lastly withoutAnyCategories() which find posts without ANY attached categories at all. All scopes are created equal, with same signature, and returns query builder.

Category translations

Manage category translations with ease as follows:

Note: Check Translatable package for further details.


Manage your nodes/nestedsets

Inserting categories

Moving 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 save on model (some methods implicitly call save and return boolean result of the operation).

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 hasMoved method:

Creating categories

When you simply create a category, it will be appended to the end of the tree:

In this case the category is considered a root which means that it doesn't have a parent.

Making a root from existing category

The category will be appended to the end of the tree:

Appending and prepending to the specified parent

If you want to make category a child of other category, you can make it last or first child. Suppose that $parent is some existing category, there are few ways to append a category:

And only a couple ways to prepend:

Inserting before or after specified category

You can make $category to be a neighbor of the $neighbor category. Suppose that $neighbor is some existing category, while target category can be fresh. If target category exists, it will be moved to the new position and parent will be changed if it's required.

Building a tree from array

When using static method create on category, it checks whether attributes contains children key. If it does, it creates more categories recursively, as follows:

$category->children now contains a list of created child categories.

Rebuilding a tree from array

You can easily rebuild a tree. This is useful for mass-changing the structure of the tree. Given the $data as an array of categories, you can build the tree as follows:

There is an id specified for category with the title of foo which means that existing category will be filled and saved. If category does not exists ModelNotFoundException is thrown. Also, this category has children specified which is also an array of categories; they will be processed in the same manner and saved as children of category foo.

Category bar has no primary key specified, so it will treated as a new one, and be created.

$delete shows whether to delete categories that are already exists but not present in $data. By default, categories aren't deleted.

Retrieving categories

In some cases we will use an $id variable which is an id of the target category.

Ancestors

Ancestors make a chain of parents to the category. Helpful for displaying breadcrumbs to the current category.

Descendants

Descendants are all categories in a sub tree, i.e. children of category, children of children, etc.

Descendants can be eagerly loaded:

Siblings

Siblings are categories that have same parent.

To get only next siblings:

To get previous siblings:

Getting related models from other table

Imagine that each category has many products. I.e. HasMany relationship is established. How can you get all products of $category and every its descendant? Easy!

Now imagine that each category has many posts. I.e. morphToMany relationship is established this time. How can you get all posts of $category and every its descendant? Is that even possible?! Sure!

Including category depth

If you need to know at which level the category is:

Root category will be at level 0. Children of root categories will have a level of 1, etc. To get categories of specified level, you can apply having constraint:

Default order

Each category has it's own unique _lft value that determines its position in the tree. If you want category to be ordered by this value, you can use defaultOrder method on the query builder:

You can get categories in reversed order:

Shifting a category

To shift category up or down inside parent to affect default order:

The result of the operation is boolean value of whether the category has changed its position.

Constraints

Various constraints that can be applied to the query builder:

Descendants constraints:

Ancestor constraints:

$category can be either a primary key of the model or model instance.

Building a tree

After getting a set of categories, you can convert it to tree. For example:

This will fill parent and children relationships on every category in the set and you can render a tree using recursive algorithm:

This will output something like this:

Building flat tree

Also, you can build a flat tree: a list of categories where child categories are immediately after parent category. This is helpful when you get categories with custom order (i.e. alphabetically) and don't want to use recursion to iterate over your categories.

Getting a subtree

Sometimes you don't need whole tree to be loaded and just some subtree of specific category:

Now $tree contains children of $root category.

If you don't need $root category itself, do following instead:

Deleting categories

To delete a category:

IMPORTANT! Any descendant that category has will also be deleted!

IMPORTANT! Categories are required to be deleted as models, don't try do delete them using a query like so:

That will break the tree!

Helper methods

Checking consistency

You can check whether a tree is broken (i.e. has some structural errors):

Tree error statistics will return an array with following keys:

Fixing tree

Category tree can now be fixed if broken. Using inheritance info from parent_id column, proper _lft and _rgt values are set for every category.

Note: Check Nested Sets package for further details.

Changelog

Refer to the Changelog for a full history of the project.

Support

For support, please use the following channels:

Contributing & Protocols

Thank you for considering contributing to this project! The contribution guide can be found in CONTRIBUTING.md.

Bug reports, feature requests, and pull requests are very welcome.

Security Vulnerabilities

We take security seriously. If you discover a security vulnerability within Angkor Categories, please send an email to [email protected]. All security vulnerabilities will be promptly addressed.

Please do not publicly disclose the issue until it has been addressed by our team. We appreciate your responsible disclosure and will work with you to understand and resolve any concerns.

About Rinvex

Rinvex is a software solutions startup, specialized in integrated enterprise solutions for SMEs established in Alexandria, Egypt since June 2016. We believe that our drive The Value, The Reach, and The Impact is what differentiates us and unleash the endless possibilities of our philosophy through the power of software. We like to call it Innovation At The Speed Of Life. That’s how we do our share of advancing humanity.

License

This software is released under The MIT License (MIT).

Original Package Copyright (c) 2016-2022 Rinvex LLC Modified Package Copyright (c) 2023 Angkor Team

This package is a fork of rinvex/laravel-categories.


All versions of laravel-categories with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/console Version ^10.0 || ^11.0 || ^12.0
illuminate/database Version ^10.0 || ^11.0 || ^12.0
illuminate/support Version ^10.0 || ^11.0 || ^12.0
kalnoy/nestedset Version ^6.0
angkor/laravel-support Version ^8.0
spatie/laravel-sluggable Version ^3.5
symfony/console Version ^6.4 || ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package angkor/laravel-categories contains the following files

Loading the files please wait ....