Categorizable

Categorizable

Model Categorizable Management.

Moamen Eltouny (Raggi)
PHP Version : >= 7.2 Laravel Version : >= 6.0 License Support
Source Packagist Version Packagist Downloads

Install

Install the latest version using Composer

composer require pharaonic/laravel-categorizable
php artisan migrate

OptionalPublish the config & migrations.

php artisan vendor:publish --tag=laravel-categorizable
Category Model

Depends on Translatable, Sluggable.

You can create your category or manipulate it.

use Pharaonic\Laravel\Categorizable\Models\Category;

$category = Category::create(['type' => 'products']); // type is nullable
$category->translateOrNew('en')->title = 'First Category';          // required
$category->translateOrNew('en')->description = 'Description Here';  // nullable
$category->save();

Incuding Categorizable into Model.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Categorizable\Traits\Categorizable;

class Product extends Model
{
    use Categorizable;
    ...
}
Methods

You can use same methods and relationship
with the category itself to assign a parent category to it.

categorize ($categories)

You can use any of this ways to assign a category to the model.

$product->categorize(1, 2);         // with Categories IDs
$product->categorize([1, 2]);       // with Array of Categories IDs

$product->categorize(Category::all());      // with Eloquent Collection of Categories

$product->categorize($category1, $category2);       // with Models
$product->categorize([$category1, $category2]);     // with Array of Models

decategorize ($categories)

You can use any of this ways to detach a model from categories.

$product->decategorize(1, 2);         // with Categories IDs
$product->decategorize([1, 2]);       // with Array of Categories IDs

$product->decategorize(Category::all());      // with Eloquent Collection of Categories

$product->decategorize($category1, $category2);       // with Models
$product->decategorize([$category1, $category2]);     // with Array of Models

syncCategories ($categories)

You can use any of this ways to detach a model from all unnecessary categories
and attach/keep it to all necessary categories.

$product->syncCategories(1, 3);         // with Categories IDs
$product->syncCategories([1, 3]);       // with Array of Categories IDs

$product->syncCategories(Category::whereIn('id', [1, 3])->get());      // with Eloquent Collection of Categories

$product->syncCategories($category1, $category3);       // with Models
$product->syncCategories([$category1, $category3]);     // with Array of Models
Relationships

categories

Getting all categories (Parents).

dump($product->categories);
            
// OR with specific types : Accept single (string || array of strings)
dump($product->categories(['category types here', 'here too ...']));

// Returns Illuminate\Database\Eloquent\Collection

category

Getting a single category (Parent).

dump($product->category);
// Returns Pharaonic\Laravel\Categorizable\Models\Category
Category Children

Config File

You have to assign your relationships here like products in config/Pharaonic/categorizable.php.

return [

    /**
        *      Get all of the models are assined a specific Category
        *               Getting Category children
        */
    
    'children' => [
        'products'      => App\Models\Product::class,
    ]

];

Then you can get the category children like products.

dump($category->products);
// Returns Illuminate\Database\Eloquent\Collection