dewan / dewan-multilang-slug
它接收一个字符串、一个模型、一个字段和一个分隔符,如果数据库中已存在相同的slug,则返回一个带数字或随机字符串的唯一字符串。
v3.0.2
2024-08-10 06:38 UTC
Requires
- php: >=7.4
README
这里是文档
安装
运行此命令以安装包。
composer require dewan/dewan-multilang-slug
运行此命令以配置发布。
php artisan vendor:publish --tag=dewan-multilang-slug-config --force
基本使用
示例 #01 - 从标题中生成唯一的slug
假设我们有一个 Team
类,现在如果我们传递 title
并从该标题生成 slug
,那么它将根据slug字段生成唯一的slug,然后 -
use App\Models\Team; // First time create post with title Simple Post MultilangSlug::makeSlug(Team::class, 'Team title'); // Output: team-title // Second time create post with title Simple Post MultilangSlug::makeSlug(Team::class, 'Team title'); // Output: team-title-1 // Third time create post with title Simple Post MultilangSlug::makeSlug(Team::class, 'Team title'); // Output: team-title-2
如果我们传递第三个参数,并且传递 title
并从该标题生成 slug
,那么它将根据 team_slug 字段生成唯一的slug,然后 -
use App\Models\Team; // First time create post with title Simple Post MultilangSlug::makeSlug(Team::class, 'Team title', 'team_slug'); // Output: team-title // Second time create post with title Simple Post MultilangSlug::makeSlug(Team::class, 'Team title', 'team_slug'); // Output: team-title-1 // Third time create post with title Simple Post MultilangSlug::makeSlug(Team::class, 'Team title', 'team_slug'); // Output: team-title-2
示例-1 - Team或任何模型轻松生成唯一的slug
public function store(array $data): Team|null { $title = "here is title"; $data = Team::create([ "title" => $title, 'slug' => MultilangSlug::makeSlug(Team::class, $title), ]); return $data; }
示例-2 - Post或任何模型轻松生成唯一的slug
public function create(array $data): Post|null { if (empty($data['slug'])) { $data['slug'] = MultilangSlug::makeSlug(Post::class, $data['name']); } return Post::create($data); }
API文档
生成方法 -
MultilangSlug::makeSlug($model, $slugText, $field, $divider);
/** * Generate a Unique Slug. * * @param object $model * @param string $slugText * @param string $field * @param string $divider * * @return string * @throws \Exception */ public function makeSlug( $model, $slugText, $field, $divider = null ): string
配置
<?php return [ /* |-------------------------------------------------------------------------- | Slug default separator. |-------------------------------------------------------------------------- | | If no separator is passed, then this default separator will be used as slug. | */ 'separator' => '-', /* |-------------------------------------------------------------------------- | Slug random text or number |-------------------------------------------------------------------------- | | If slug already exist then by default slug will generated by random string like. | test-zFU, test-W1U, test-0FR .... | | If false then slug will generated by number like | test-1, test-2, test-3 .... test-100 | */ 'unique_slug' => true, /* |-------------------------------------------------------------------------- | Slug random text limit |-------------------------------------------------------------------------- | | Default 3 text key added in slug, the slug will generated like | test-zFU, test-W1U, test-0FR .... | */ 'random_text' => 3, /* |-------------------------------------------------------------------------- | Slug max count limit |-------------------------------------------------------------------------- | | Default 100, slug will generated like | test-1, test-2, test-3 .... test-100 | */ 'max_count' => 100, ];
贡献
您可以创建任何Pull request。