PHPackages                             melsaka/categorist - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. melsaka/categorist

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

melsaka/categorist
==================

Implementing Categories system for Laravel's Eloquent models.

11PHP

Since Sep 16Pushed 2y ago1 watchersCompare

[ Source](https://github.com/melsaka/categorist)[ Packagist](https://packagist.org/packages/melsaka/categorist)[ RSS](/packages/melsaka-categorist/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Categorist
==========

[](#categorist)

Categorist is a laravel package designed to handle hierarchical categorization for various models within your application. It allows you to organize your data into categories and provides an easy-to-use interface to perform operations like adding, editing, and querying categories.

Installation
------------

[](#installation)

Add the package to your Laravel app via Composer:

```
composer require melsaka/categorist
```

Register the package's service provider in config/app.php.

```
'providers' => [
    ...
    Melsaka\Categorist\CategoristServiceProvider::class,
    ...
];
```

Run the migrations to add the required table to your database:

```
php artisan migrate
```

Add `Categorized` trait to the model that you want categorize:

```
use Melsaka\Categorist\Categorized;

class Post extends Model
{
    use Categorized;

    // ...
}
```

Configuration
-------------

[](#configuration)

To configure the package, publish its configuration file:

```
php artisan vendor:publish --tag=categorist
```

You can then modify the configuration file to change the categories table name if you want, default: `categories`.

Usage
-----

[](#usage)

### Adding a New Category

[](#adding-a-new-category)

You can add a new category for a specific model, such as **Post**, by using the `Category::add()` method. If a category with the same **slug** and **type** already exists, it will be **returned** instead. Here's an example:

```
use Melsaka\Categorist\Models\Category;

$data = [
    'name' => 'Foo',
    'slug' => 'foo',
    'children' => [
        [
            'name' => 'Bar',
            'slug' => 'bar',
            'children' => [
                [
                    'name' => 'Baz',
                    'slug' => 'baz',
                ],
            ],
        ],
    ],
];

$category = Category::add(Post::class, $data);
```

If you don't want the existed record to be returned:

```
$category = Category::add(Post::class, $data, false); // will throw duplicate entry error if record exists
```

### Retrieving Models Related to a Category

[](#retrieving-models-related-to-a-category)

You can retrieve all models related to a specific category using the `categorized()` method:

```
$posts = $category->categorized();
```

### Checking if a Category is Newly Added

[](#checking-if-a-category-is-newly-added)

You can check whether a category is newly added or if it already existed and was returned using the `isNew()` method:

```
$isNew = Category::isNew($category);
```

### Editing a Category

[](#editing-a-category)

To edit a category, use the `Category::edit()` method. Pass the category instance and an array of data to update:

```
use Melsaka\Categorist\Models\Category;

$data = [
    'name' => 'New Category Name',
];

Category::edit($category, $data);
```

### Removing a Category

[](#removing-a-category)

You can remove a category using the `Category::remove()` method. Pass either a category instance or its ID:

```
Category::remove($category);
```

### Synchronizing Categories with a Model

[](#synchronizing-categories-with-a-model)

To synchronize categories with a model, you can use the `Category::syncWith()` method. This associates a category with a model:

```
Category::syncWith($post, $category);
```

### Checking if a Model has a Category

[](#checking-if-a-model-has-a-category)

You can check whether a model has a specific category using the `Category::has()` method:

```
$hasCategory = Category::has($post, $category);
```

### Listing Categories

[](#listing-categories)

To list all categories of a specific type, you can use the `Category::list()` method:

```
$categories = Category::list(Post::class);
```

### Retrieving a Category Tree

[](#retrieving-a-category-tree)

To retrieve a hierarchical list of all categories for a specific type, you can use the `Category::treeList()` method:

```
$categoryTree = Category::treeList(Post::class);
```

### Working with Model Relationships

[](#working-with-model-relationships)

You can retrieve categories associated with a model using Eloquent relationships. For example, to retrieve categories for a `Post` model:

```
$postWithcategories = Post::with('categories')->get();

// or retrieve only the categories

$postCategories = $post->categories;

$postCategories = Category::ofThis($post);

$firstCategory = $post->firstCategory();
```

To retrieve the parent categories of a model, you can use the `parentCategories()` method:

```
$parentCategories = $post->parentCategories();
```

To retrieve a hierarchical list of categories associated with a model, you can use the `treeCategories()` method:

```
$treeCategories = $post->treeCategories();
```

You can retrieve a list of category names associated with a model using the `categoriesList()` method:

```
$categoryNames = $post->categoriesList();
```

You can `attach`, `detach`, or `sync` multiple categories to a model using the following methods:

```
// Attach categories to a model
$post->attachCategories($category);

// Detach categories from a model
$post->detachCategories($category);

// Sync categories with a model
$post->syncCategories($category);

// Check if a model has specific categories
$post->hasCategories($cat, $category);

// Check if a model has all of the specified categories
$post->hasAllCategories($cat, $category);
```

### Additional Methods

[](#additional-methods)

You can also retrieve related data using additional methods:

```
// Retrieve all models related to a category
$posts = $category->categorized()->get();

// Retrieve the parent of a category
$parent = Category::with('parent')->get();

// Retrieve the children of a category
$children = Category::with('children')->get();

// Retrieve the ancestors of a category
$ancestors = Category::with('ancestors')->get();

// Retrieve the descendants of a category
$descendants = Category::with('descendants')->get();
```

**Note**: Check [**Nested Sets**](https://github.com/lazychaser/laravel-nestedset) package for **more methods** and **details**.

License
-------

[](#license)

This package is released under the MIT license (MIT).

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity21

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6c7cf52b7cdfb096062f2e3862d4d8f45f9b8b5af8bc8d06e5ed74dcb5316682?d=identicon)[melsaka](/maintainers/melsaka)

---

Top Contributors

[![melsaka](https://avatars.githubusercontent.com/u/16939366?v=4)](https://github.com/melsaka "melsaka (3 commits)")

### Embed Badge

![Health badge](/badges/melsaka-categorist/health.svg)

```
[![Health](https://phpackages.com/badges/melsaka-categorist/health.svg)](https://phpackages.com/packages/melsaka-categorist)
```

###  Alternatives

[mauricius/laravel-htmx

Laravel helper library for Htmx

363112.7k1](/packages/mauricius-laravel-htmx)[bodunde/geocoder

A laravel package that helps you with geocoding addresses and reverse geocoding coordinates using the google maps api. It also helps in calculating the distance between two locations using the Hervasine formula and the coordinates of the locations

247.7k](/packages/bodunde-geocoder)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
