Skip to content

[Proposal] Add Scoped Categories via type JSONB Column for Model-Specific Categorization #148

@long-blade

Description

@long-blade

Proposal:

Extend the existing category system by introducing category scoping per Laravel model. This enables defining categories that apply only to specific models, while still supporting global, system-wide categories.

Add a nullable type column of JSONB type to the categories table. The column stores an array of fully-qualified model class names. When the array is empty or null, the category is considered global. When populated, the category applies only to the listed models.

This design allows retrieving categories based on model scope and avoids mixing category sets between unrelated models. It also supports multi-model assignment when intentional.

Example Migration Change:

$table->jsonb('type')->nullable();

Example Usage:

Assign category scope:

$category->type = [
    App\Models\Post::class,
    App\Models\Video::class,
];
$category->save();

Query scoped categories:

Category::whereJsonContains('type', Post::class)->get();

Expected behavior:

• If type is null or empty: category is global
• If type contains one or more class names: category is returned only for those models
• Packages and applications may retrieve categories per model without additional polymorphic structures

Rationale:

This retains backward compatibility, avoids schema complexity, and offers flexible multi-model scoping using native PostgreSQL JSONB operations. It enables building model-specific category pickers, filters, and trees without altering core behavior for existing users.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions