定制 robotsinside/laravel-categories 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

robotsinside/laravel-categories

Composer 安装命令:

composer require robotsinside/laravel-categories

包简介

A package for categorising Laravel Eloquent models.

README 文档

README

Latest Version on Packagist Total Downloads CI License: MIT

A simple package for categorising Eloquent models in Laravel. This package is a sibling of Laravel Tags, which can be used to tag Eloquent models. The API is the same as this one.

Table of contents

Installation

  1. Install using Composer
composer require robotsinside/laravel-categories
  1. Optionally register the service provider in config/app.php
/*
* Package Service Providers...
*/
\RobotsInside\Categories\CategoriesServiceProvider::class,

Auto-discovery is enabled, so this step can be skipped.

  1. Publish the migrations
php artisan vendor:publish --provider="RobotsInside\Categories\CategoriesServiceProvider" --tag="migrations"
  1. Migrate the database. This will create two new tables; categories and categorisables
php artisan migrate

Usage

Use the RobotsInside\Categories\Categorisable trait in your models.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use RobotsInside\Categories\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\Categories\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\Categories\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\Categories\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\Categories\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

Coffee Time

Will work for ☕☕☕

Buy Me A Coffee

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 822
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固