robotsinside / laravel-categorise-it
A package for categorising Laravel Eloquent models.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: >=7.0.0
- illuminate/database: >=5.0
- illuminate/support: >=5.0
Requires (Dev)
- orchestra/testbench: ^5.1
- phpunit/phpunit: ^9.1
This package is auto-updated.
Last update: 2020-12-06 02:53:49 UTC
README
A simple package for categorising Eloquent models in Laravel. This package is a sibling of Laravel Tag It, which can be used to tag Eloquent models. The API is pretty much the same as this one.
Installation
- Install using Composer
composer require robotsinside/laravel-categorise-it
- Optionally register the service provider in
config/app.php
/* * Package Service Providers... */ \RobotsInside\CategoriseIt\CategoriseItServiceProvider::class,
Auto-discovery is enabled, so this step can be skipped.
- Publish the migrations
php artisan vendor:publish --provider="RobotsInside\CategoriseIt\CategoriseItServiceProvider" --tag="migrations"
- Migrate the database. This will create two new tables;
categories
andcategorisables
php artisan migrate
Usage
Use the RobotsInside\CategoriseIt\Categorisable
trait in your models.
<?php namespace App; use Illuminate\Database\Eloquent\Model; use RobotsInside\CategoriseIt\Categorisable; class Post extends Model { use Categorisable; }
You are now ready to categorise your models. Models can be categorised by passing an integer, array of integers, a model instance or a collection of models.
<?php use App\Post; use Illuminate\Support\Facades\Route; use RobotsInside\CategoriseIt\Models\Category; Route::get('/', function () { // Retrieve a new or existing category $category1 = (new Category())->resolve('Category 1'); $category2 = (new Category())->resolve('Category 2'); // Or, retrieve a collection of new or existing categories $categories = (new Category())->resolveAll(['Category 1', 'Category 2', 'Category 3']) $post = new Post(); $post->title = 'My blog'; $post->save(); $post->categorise($category1); // Or $post->categorise(['category-1']); // Or $post->categorise([1, 2]); // Or $post->categorise(Category::get()); });
Uncategorising models is just as simple.
<?php use App\Post; use Illuminate\Support\Facades\Route; use RobotsInside\CategoriseIt\Models\Category; Route::get('/', function () { $category1 = Category::find(1); $post = Post::where('title', 'My blog')->first(); $post->uncategorise($category1); // Or $post->uncategorise(['category-1']); // Or $post->uncategorise([1, 2]); // Or $post->uncategorise(Category::get()); // Or $post->uncategorise(); // remove all categories });
Scopes
Each time a RobotsInside\CategoriseIt\Models\Category
is used, the count
column in the categories
table is incremented. When a category is removed, the count is decremented until it is zero.
This packages comes with a number of pre-defined scopes to make queries against the count
column easier, namely >=
, >
, <=
and <
contstrains, for example:
Category::usedGte(1);
Category::usedGt(2);
Category::usedLte(3);
Category::usedLt(4);
The RobotsInside\CategoriseIt\Models\Categorisable
model contains a scope to constrain records created within a given time frame. This scope supports human readable values including days
, months
and years
in both singular and plural formats, for example:
Categorisable::categorisedWithin('7 days');
Categorisable::categorisedWithin('1 month');
Categorisable::categorisedWithin('2 years');
Security
If you discover any security related issues, please email robertfrancken@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.