ka4ivan / laravel-sluggable
Generate slugs when saving Eloquent models (multilingualism, multigroups, uniqueness)
1.0.1
2025-03-07 09:55 UTC
Requires
- php: ^8.0.2
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
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
.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Ka4ivan\ModelReleases\Models\Traits\HasReleases; class Article extends Model { use HasUuids, 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