uptoolkit / laravel-categories
Associate Eloquent Models with Categories
1.0.0
2024-01-23 16:52 UTC
Requires
- php: >=7.1.0
- kalnoy/nestedset: >=4.3
- spatie/laravel-sluggable: >=2.1
Requires (Dev)
- graham-campbell/testbench: >=5.0
- mockery/mockery: >=1.0
- phpunit/phpunit: >=6.5
This package is auto-updated.
Last update: 2024-10-23 18:35:14 UTC
README
Installation
Require this package, with Composer, in the root directory of your project.
$ composer require uptoolkit/laravel-categories
To get started, you'll need to publish the vendor assets and migrate:
php artisan vendor:publish --provider="Uptoolkit\Categories\CategoriesServiceProvider" && php artisan migrate
Usage
Nested Sets
Check lazychaser/laravel-nestedset to learn how to create, update, delete, etc. categories.
Setup a Model
<?php namespace App; use Uptoolkit\Categories\Traits\HasCategories; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasCategories; }
Examples
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
$ phpunit
Security
If you discover a security vulnerability within this package, please send an e-mail to hello@brianfaust.me. All security vulnerabilities will be promptly addressed.