Download the PHP package jetcod/laravel-slugify without Composer
On this page you can find all versions of the php package jetcod/laravel-slugify. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jetcod/laravel-slugify
More information about jetcod/laravel-slugify
Files in jetcod/laravel-slugify
Package laravel-slugify
Short Description A Laravel package that automatically generates slugs for model attributes, simplifying the creation of URL-friendly slugs.
License MIT
Informations about the package laravel-slugify
Laravel Slugify Package
Overview
The jetcod\laravel-slugify
package simplifies the generation and management of slugs for Eloquent models in Laravel applications. This package utilizes the HasSlug
trait to automatically create and update slugs based on your model's attributes, with flexible configuration options.
Installation
Requirements
- PHP ^7.4 | ^8.0
- Laravel 8.0+
Step 1: Install via Composer
To install the package, run the following command:
Step 2: Configure your model
In any model where you want to use the slugging functionality, use the HasSlug
trait and implement getSlugConfig()
method to configure the slug options.
Usage
Setting Up the Sluggable Model
To start using slugs in your model, follow these steps:
- Use the Trait: In your model, use the
HasSlug
trait. - Define Slug Configuration: Implement the
getSlugConfig()
method in your model. This method should return a SlugOptions object where you define your sluggable configuration. - Set the
$sluggables
property: Define an array of model attributes that should be used to generate the slug.
Here’s an example:
Available Configuration Options
- doNotGenerateSlugsOnCreate(): Call this method if you want to avoid generating slugs on model creation.
- doNotGenerateSlugsOnUpdate(): Call this method if you want to avoid generating slugs when the model is updated.
- slugColumn(string): Define the column name where the slugs will be stored. The defined column type should be
json
. - slugSeparator(string): Character separator between words in the slug. (Default value is '-')
- maximumLength(int): Maximum character length of the slug.
- avoidDuplicates(): Call this method to generate unique slugs.
Note: Calling slugColumn() is required while defining the slug options.
Example: Creating and Updating a Model with Slug Generation
When you create or update a model with HasSlug configured, the slug is automatically generated and saved according to the options you've specified.
Example: Generating Unique Slugs
To ensure unique slugs, you can use the avoidDuplicates()
method in your SlugOptions:
Then when you create or update a model, the slug will be generated and saved as a unique value.
Note: It also considers the maximum length of the slug while generating unique slug strings.
Testing
Run your tests to verify that slugs are generated as expected:
Run code coverage analysis to generate a coverage report. This will generate a coverage report in the coverage
directory.
Run PHPStan to check for potential issues in the code:
License
This package is open-source software licensed under the MIT License.
All versions of laravel-slugify with dependencies
illuminate/support Version ^8.0|^9.0|^10.0|^11.0
jetcod/data-transport Version ^1.2