1. Go to this page and download the library: Download ka4ivan/laravel-sluggable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ka4ivan / laravel-sluggable example snippets
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',
],
];
use Ka4ivan\Sluggable\Models\Traits\HasSlugs;
class Article extends Model
{
use HasSlugs;
}
/**
* 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());
}
// 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);
}
/** @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;