mayoz/laravel-categorizable
Composer 安装命令:
composer require mayoz/laravel-categorizable
包简介
Polymorphic categorizable for Laravel
README 文档
README
Easily add the ability to category your Eloquent models in Laravel 5.
Installation
You can install the package via composer:
composer require mayoz/laravel-categorizable
Register the service provider in your config/app.php configuration file:
'providers' => [ ... Mayoz\Categorizable\CategorizableServiceProvider::class, ... ];
You can publish the migration with:
php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="migrations"
The migration has been published you can create the categories and categorizable tables. You are feel free for added new fields that you need. After, run the migrations:
php artisan migrate
Usage
Suppose, you have the Post model as follows:
<?php namespace App; use Mayoz\Categorizable\Categorizable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use Categorizable; }
Associate new categories for the Post model:
$post = Post::find(1); $post->categorize([1, 2, 3, 4, 5]); return $post;
Now, the post model is associated with categories ids of 1, 2, 3, 4 and 5.
Remove the existing category association for the Post model:
$post = Post::find(1); $post->uncategorize([3, 5]); return $post;
The post model is associated with categories ids of 1, 2 and 4.
Rearrange the category relationships for the Post model:
$post = Post::find(1); $post->recategorize([1, 5]); return $post;
The post model is associated with categories ids of 1 and 5.
Extending
I suggest, you always extend the Category model to define your relationships directly. Create you own Category model:
<?php namespace App; use Mayoz\Categorizable\Category as BaseCategory; class Category extends BaseCategory { /** * Get all posts for the relation. * * @return \Illuminate\Database\Eloquent\Relations\MorphedByMany */ public function posts() { return $this->categorized(Post::class); } }
You publish the package config:
php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="config"
This is the contents of the published config file:
<?php return [ /* |-------------------------------------------------------------------------- | Model Namespace |-------------------------------------------------------------------------- | | Change these values when you need to extend the default category model | or if the user model needs to be served in a different namespace. | */ 'category' => App\Category::class, ];
That is all. Now let's play for relationship query with the category.
/** * Respond the post * * @param \App\Category $category * @return \Illuminate\Http\Response */ public function index(Category $category) { return $category->posts()->paginate(10); }
If we did not extend the Category model, as had to use;
/** * Respond the post * * @param \App\Category $category * @return \Illuminate\Http\Response */ public function index(Category $category) { return $category->categorize(Post::class)->paginate(10); }
License
This package is licensed under The MIT License (MIT).
mayoz/laravel-categorizable 适用场景与选型建议
mayoz/laravel-categorizable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 155 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「polymorphic」 「categorizable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mayoz/laravel-categorizable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mayoz/laravel-categorizable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mayoz/laravel-categorizable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Rinvex Categories is a polymorphic Laravel package, for category management. You can categorize any eloquent model with ease, and utilize the power of Nested Sets, and the awesomeness of Sluggable, and Translatable models out of the box.
Laravel Ownership simplify management of Eloquent model's owner.
Implement a polymorphic morphMany relationship for managing attachments within Filament
A Laravel package providing a full-featured, framework-agnostic commenting system for any Eloquent model.
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
Rinvex Categories is a polymorphic Laravel package, for category management. You can categorize any eloquent model with ease, and utilize the power of Nested Sets, and the awesomeness of Sluggable, and Translatable models out of the box.
统计信息
- 总下载量: 155
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-25