faustbrian/laravel-categorizable

This package is abandoned and no longer maintained. The author suggests using the artisanry/categories package instead.

Categories for Eloquent Models

7.2.0 2019-08-28 13:01 UTC

README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

This package was created by, and is maintained by Brian Faust, and provides categories for Laravel Eloquent Models.

Installation

composer require kodekeep/laravel-categories

Usage

Check lazychaser/laravel-nestedset to learn how to create, update, delete, etc. categories.

Setup a Model

<?php

namespace App;

use KodeKeep\Categories\Concerns\HasCategories;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasCategories;
}

Get an array with ids and names of categories (useful for drop-downs)

$post->categoriesList();

Attach the Post Model these Categories

$post->syncCategories([Category::find(1), Category::find(2), Category::find(3)]);

Detach the Post Model from these Categories

$post->syncCategories([]);

Detach the Post Model from all Categories and attach it to the new ones

$post->syncCategories([Category::find(1), Category::find(3)]);

Attach the Post Model to the given Category

$post->assignCategory(Category::find(1));

Detach the Post Model from the given Category

$post->removeCategory(Category::find(1));

Get all Posts that are attached to the given Category

Category::first()->entries(Post::class)->get();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover a security vulnerability within this package, please send an e-mail to hello@kodekeep.com. All security vulnerabilities will be promptly addressed.

Credits

This project exists thanks to all the people who contribute.

Support Us

We invest a lot of resources into creating and maintaining our packages. You can support us and the development through GitHub Sponsors.

License

Laravel Categories is an open-sourced software licensed under the MPL-2.0.