承接 ehsanmoradi/categorizable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ehsanmoradi/categorizable

Composer 安装命令:

composer require ehsanmoradi/categorizable

包简介

Implementing Categories system for Laravel's Eloquent models.

README 文档

README

This Package enables you to Categorize your Eloquent Models. just use the trait in the model and you're good to go.

Requirements

  • PHP 7.2+
  • Laravel 8+

Installation

composer require ehsanmoradi/categorizable

Publish and Run the migrations

php artisan vendor:publish --provider="EhsanMoradi\Categorizable\CategorizableServiceProvider"

php artisan migrate

Laravel Categorizable package will be auto-discovered by Laravel. and if not: register the package in config/app.php providers array manually.

'providers' => [
	...
	\EhsanMoradi\Categorizable\CategorizableServiceProvider::class,
],

Setup models - just use the Trait in the Model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use EhsanMoradi\Categorizable\Categorizable;

class Post extends Model
{
	use Categorizable;
}

Usage

first of all we need to create some Category to work with. Laravel Categorizable package relies on another package called laravel-nestedset that is responsible for creating, updating, removing and retrieving single or nested categories. Here i demonstrate how to create categories and assign one as the other's child.. but you can always refer to package's repository for full documentation. https://github.com/lazychaser/laravel-nestedset

use App\Post;
use EhsanMoradi\Categorizable\Category;

// first we create a bunch of categories

// create "BackEnd" category
Category::create([
	'name' => 'BackEnd'
]);

// create "PHP" category
Category::create([
	'name' => 'PHP'
]);

// create "FrontEnd" category
Category::create([
	'name' => 'FrontEnd'
]);

// create "Test" Category (alternative way)
$test = new Category();
$test->name = 'Test';
$test->save();


// assign "PHP" as a child of "BackEnd" category
$parent = Category::findByName('BackEnd');
$child = Category::findByName('PHP');
$parent->appendNode($child);

// delete "Test" Category
$testObj = Category::findByName('Test');
$testObj->delete();



//  assuming that we have these variables
$post = Post::first();

// 3 different ways of getting a category's instance
$backendCategory = Category::findById(1);	// 'BackEnd'
$phpCategory = Category::findByName('PHP');	// 'PHP'
$frontendCategory = Category::find(3);		// 'FrontEnd'

Attach the post to category

    $post->attachCategory($phpCategory);

Detach the post from a category

    $post->detachCategory($phpCategory); 

Attach the post to list of categories

    $post->syncCategories([
	    $phpCategory,
	    $backendCategory
	    ]); 

Detach the post from all categories

    $post->syncCategories([]); 

Sync the categories attached to a post

    $post->syncCategories([
	    $frontendCategory
	    ]); 

    // removes attached categories & adds the given categories

Check if post is attached to categories (boolean)

    // single use case
    $post->hasCategory($phpCategory);

    // multiple use case
    $post->hasCategory([
	    $phpCategory,
	    $backendCategory
	    ]);

    // return boolean

List of categories attached to the post (array)

    $post->categoriesList();

    // return array [id => name]

List of categories IDs attached to the post (array)

    $post->categoriesId();

    // return array

Get all posts attached to given category (collection)

    $categoryPosts = Category::find(1)
	    ->entries(Post::class)
	    ->get();

    // return collection

Relationships

categories() Relationship

    $postWithCategories = Post::with('categories')
	    ->get();

     // you have access to categories() relationship in case you need eager loading
    

parent Relationship

    $category = Post::first()->categories()->first();
    
    $category->parent;
    // return the category's parent if available

children Relationship

    $category = Post::first()->categories()->first();
    
    $category->children;
    // return the category's children if any

ancestors Relationship

    $category = Post::first()->categories()->first();
    
    $category->ancestors;
    // return the category's ancestors if any

descendants Relationship

    $category = Post::first()->categories()->first();
    
    $category->descendants;
    // return the category's descendants if any

Credits

ehsanmoradi/categorizable 适用场景与选型建议

ehsanmoradi/categorizable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 09 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「eloquent」 「Laravel Categorizable」 「categorizable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ehsanmoradi/categorizable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ehsanmoradi/categorizable 我们能提供哪些服务?
定制开发 / 二次开发

基于 ehsanmoradi/categorizable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-30