ka4ivan/laravel-sluggable
Composer 安装命令:
composer require ka4ivan/laravel-sluggable
包简介
Generate slugs when saving Eloquent models (multilingualism, multigroups, uniqueness)
README 文档
README
📖 Table of Contents
Installation
- Require this package with composer
composer require ka4ivan/laravel-sluggable
- Publish package resource:
php artisan vendor:publish --provider="Ka4ivan\Sluggable\ServiceProvider"
- config
- migration
This is the default content of the config file:
<?php return [ /** * Models for which slugs will be created using the command */ 'models' => [ // \App\Models\Page::class, // \App\Models\Product::class, ], /** * Slug model */ 'model' => \Ka4ivan\Sluggable\Models\Slug::class, /** * What attributes do we use to build the slug? * This can be a single field, like "name" which will build a slug from: * * $model->name; * * Or it can be an array of fields, like ["name", "company"], which builds a slug from: * * $model->name . ' ' . $model->company; */ 'source_columns' => ['name'], /** * The sign with which the slag will be divided */ 'slug_separator' => '-', /** * The maximum length of the slug */ 'max_length' => 255, /** * Do you need to generate a slug if the 'source_columns' is empty? */ 'generate_if_empty_source' => true, /** * Is the slug unique among all models? */ 'unique_for_all_models' => false, 'groups' => [ /** * Do you need groups for slugs (multilingualism, etc.)? */ 'active' => false, /** * Is the slug unique in groups of one model? */ 'unique' => false, 'list' => ['uk', 'en', 'es'], 'default' => 'en', ], ];
- Run migration:
php artisan migrate
Usage
Preparing your model
To associate slugs with a model, the model must implement the following trait: HasSlugs.
use Ka4ivan\Sluggable\Models\Traits\HasSlugs; class Article extends Model { use HasSlugs; }
Base relationships
/** * Define a morph many relationship for slugs. */ public function slugs(): MorphMany { return $this->morphMany(self::getSlugModel(), 'model'); } /** * Define a morph one relationship for the main slug. * * @param string|null $group The group of the slug (optional) * @return MorphOne */ public function slugable(?string $group = null): MorphOne { return $this->morphOne(self::getSlugModel(), 'model') ->where('group', $group ?: $this->getDefaultGroup()); }
Route binding
You can use route binding if you wish.
// Routes Route::get('aricles/{aricle:slug}', [\App\Http\Client\Api\Controllers\ArticleController::class, 'show']); // Controller public function show(Request $request, Article $article) { return ArticleResource::make($article); }
Base model usage
Individual settings for the model
By default, all settings are taken from the slug config. However, each model can be configured separately if necessary.
/** @var string[] Define in your model */ // public $slugSourceColumns = ['name']; // public $slugGroups = ['uk', 'en']; // public $slugDefaultGroup = 'uk'; // public $slugSeparator = '-'; // public $slugMaxLength = 255; // public $slugGenerateIfEmptySource = true; // public $slugMultiGroups = true;
Search by slug
Article::whereSlug('article-1')->first()
Slug generate command
php artisan slug:generate
Or with an additional force parameter to regenerate all slugs, even for models that already had them before.
php artisan slug:generate --force=true
ka4ivan/laravel-sluggable 适用场景与选型建议
ka4ivan/laravel-sluggable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sluggable」 「model」 「seo」 「slug」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ka4ivan/laravel-sluggable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ka4ivan/laravel-sluggable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ka4ivan/laravel-sluggable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A simple Cake3 plugin to slug fields and find records by slug.
A library to extend Object capabilities.
Interfaces for web resource models and services to retrieve and create them
Help to manage meta data on Laravel Eloquent model
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-26