定制 kodooy/laravel-slug-generator 二次开发

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

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

kodooy/laravel-slug-generator

最新稳定版本:v1.0.1

Composer 安装命令:

composer require kodooy/laravel-slug-generator

包简介

Laravel package for automatic slug generation using traits

README 文档

README

A Laravel package that provides automatic slug generation for Eloquent models using traits.

Installation

composer require kodooy/laravel-slug-generator

Database Setup

Your Eloquent model's database table must have a slug field. Add it to your migration:

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('slug')->unique()->index();
    $table->timestamps();
});

If adding to an existing table:

Schema::table('posts', function (Blueprint $table) {
    $table->string('slug')->unique()->index();
});

Usage

Add the Slugable trait to your Eloquent model:

use Kodooy\SlugGenerator\Traits\Slugable;

class Post extends Model
{
    use Slugable;

    // The trait will automatically generate slugs from the 'name' attribute
    // and use 'slug' as the route key
}

Customization

Override these methods to customize behavior:

class Post extends Model
{
    use Slugable;

    // Use different attribute for slug generation
    protected function slugableAttribute()
    {
        return 'title'; // default is 'name'
    }

    // Preserve existing slugs on update
    protected function preserveSlugOnUpdate()
    {
        return true; // default is false
    }
}

Automatic Slug Generation

// Slug is automatically generated when creating/updating
$post = Post::create([
    'title' => 'My Blog Post',
    'content' => 'This is the content...'
]);

// Slug will be: 'my-blog-post'
echo $post->slug;

Manual Slug Generation

// Generate slug manually
$post = new Post(['title' => 'Another Great Post']);
$post->slug = $post->generateSlug();

Unique Slug Handling

// If a slug already exists, it will be made unique:
// "my-post" -> "my-post-1" -> "my-post-2" etc.

$post1 = Post::create(['title' => 'My Post']);  // slug: "my-post"
$post2 = Post::create(['title' => 'My Post']);  // slug: "my-post-1"
$post3 = Post::create(['title' => 'My Post']);  // slug: "my-post-2"

Requirements

  • PHP ^8.1
  • Laravel ^9.0|^10.0|^11.0|^12.0

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-31