PHPackages                             veiliglanceren/laravel-morph-categories - 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. [Database &amp; ORM](/categories/database)
4. /
5. veiliglanceren/laravel-morph-categories

ActiveLibrary[Database &amp; ORM](/categories/database)

veiliglanceren/laravel-morph-categories
=======================================

A Laravel package to support polymorphic category relationships for any model.

2.0.2(9mo ago)16701MITPHPPHP ^8.1CI passing

Since Jul 21Pushed 9mo agoCompare

[ Source](https://github.com/VeiligLanceren-nl/laravel-morph-categories)[ Packagist](https://packagist.org/packages/veiliglanceren/laravel-morph-categories)[ Docs](https://github.com/veiliglanceren-nl/laravel-morph-categories)[ RSS](/packages/veiliglanceren-laravel-morph-categories/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (5)Versions (10)Used By (1)

Laravel Morph Category
======================

[](#laravel-morph-category)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d4e59fcaa3165241a30710274796e5d88b3d20ac1811588dfd5c1752301793ba/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7665696c69676c616e636572656e2f6c61726176656c2d6d6f7270682d63617465676f726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/veiliglanceren/laravel-morph-category)[![Total Downloads](https://camo.githubusercontent.com/f1ee44910f669f24b35392fc05b435c3d6b23e47e0bc29fff419255649666008/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7665696c69676c616e636572656e2f6c61726176656c2d6d6f7270682d63617465676f726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/veiliglanceren/laravel-morph-category)

A Laravel package to add polymorphic category support to any model. Easily attach, detach, sync, and query categories using elegant Eloquent relationships.

---

🚀 Installation
--------------

[](#-installation)

```
composer require veiliglanceren/laravel-morph-categories
```

---

📦 Usage
-------

[](#-usage)

### 1. Add Trait to Your Model

[](#1-add-trait-to-your-model)

```
use VeiligLanceren\LaravelMorphCategories\Traits\HasCategory;

class Post extends Model
{
    use HasCategory;
}
```

### 2. Attach, Detach &amp; Sync Categories

[](#2-attach-detach--sync-categories)

```
$post = Post::find(1);
$category = MorphCategory::create(['name' => 'News']);

$post->attachCategory($category);
$post->detachCategory($category);
$post->syncCategories([$category->id]);
```

You can also use slugs or IDs:

```
$post->hasCategory('news'); // by slug
$post->hasCategory($category); // by model
$post->hasCategory($category->id); // by ID
```

---

### ✅ Use Cases

[](#-use-cases)

- Rename tables if you already use `categories` for something else.
- Replace the model class with your own `Category` or `Categoryable` implementation.

⚙️ Configuration
----------------

[](#️-configuration)

You can publish and customize the config file:

```
php artisan vendor:publish --tag=category-config
```

This will create a file at `config/category.php` with the following structure:

```
return [

    'tables' => [
        'categories' => 'categories',
        'categoryables' => 'categoryables',
    ],

    'models' => [
        'category' => \VeiligLanceren\LaravelMorphCategories\Models\MorphCategory::class,
        'categoryable' => \VeiligLanceren\LaravelMorphCategories\Models\MorphCategoryable::class,
    ],
];
```

🔁 Relationships
---------------

[](#-relationships)

```
$post->morphCategories; // Collection of MorphCategory models

$category->morphCategoryables; // MorphToMany to all related models
```

---

🔍 Query Scoping
---------------

[](#-query-scoping)

You can query models with specific categories:

```
Post::withCategory('news')->get();
Post::withCategory($category)->get();
```

---

🧪 Testing
---------

[](#-testing)

This package uses [Pest](https://pestphp.com) and [Orchestra Testbench](https://github.com/orchestral/testbench):

```
composer test
```

Or manually:

```
vendor/bin/pest
```

---

📄 License
---------

[](#-license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

---

👤 Author
--------

[](#-author)

Developed and maintained by [Niels Hamelink](https://linkedin.com/in/niels-hamelink) at [VeiligLanceren.nl](https://veiliglanceren.nl).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance57

Moderate activity, may be stable

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Every ~2 days

Total

5

Last Release

286d ago

Major Versions

1.1.0 → 2.0.02025-07-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/22b99c08291f57985d16a87bcd5f1a3f4ae2e6332b5e04736b5bf4f0f91c3d19?d=identicon)[VeiligLanceren](/maintainers/VeiligLanceren)

---

Top Contributors

[![NielsHamelink-web](https://avatars.githubusercontent.com/u/67690385?v=4)](https://github.com/NielsHamelink-web "NielsHamelink-web (15 commits)")

---

Tags

laravellaravel-12laravel-12-packagelaravel-packagemorphpolymorphictaxonomylaravelcategorytaxonomymorphpolymorphic

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/veiliglanceren-laravel-morph-categories/health.svg)

```
[![Health](https://phpackages.com/badges/veiliglanceren-laravel-morph-categories/health.svg)](https://phpackages.com/packages/veiliglanceren-laravel-morph-categories)
```

###  Alternatives

[rinvex/laravel-categories

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

470161.6k3](/packages/rinvex-laravel-categories)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[cybercog/laravel-ownership

Laravel Ownership simplify management of Eloquent model's owner.

9126.6k3](/packages/cybercog-laravel-ownership)[rinvex/laravel-tenants

Rinvex Tenants is a contextually intelligent polymorphic Laravel package, for single db multi-tenancy. You can completely isolate tenants data with ease using the same database, with full power and control over what data to be centrally shared, and what to be tenant related and therefore isolated from others.

823.5k10](/packages/rinvex-laravel-tenants)[rapidwebltd/improved-polymorphic-eloquent-builder

Attempting to use whereHas queries with standard Eloquent polymorphic relationships will fail, due to Eloquent being unable to determine the correct model to retrieve. The 'Improved Polymorphic Eloquent Builder' is a class which extends the Eloquent Builder class that is built in to Laravel 5.1. It enables limited use of the whereHas method to query Eloquent polymorphic relationships.

111.1k](/packages/rapidwebltd-improved-polymorphic-eloquent-builder)

PHPackages © 2026

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