https://github.com/paxha/laravel-sluggable
This package provides an event that will generate a unique slug when saving or creating any Eloquent model.
https://github.com/paxha/laravel-sluggable
laravel laravel-package laravel-sluggable slug sluggable
Last synced: 6 months ago
JSON representation
This package provides an event that will generate a unique slug when saving or creating any Eloquent model.
- Host: GitHub
- URL: https://github.com/paxha/laravel-sluggable
- Owner: paxha
- License: mit
- Created: 2019-12-25T09:36:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-12-17T16:17:13.000Z (over 5 years ago)
- Last Synced: 2025-08-01T14:15:01.944Z (11 months ago)
- Topics: laravel, laravel-package, laravel-sluggable, slug, sluggable
- Language: PHP
- Homepage:
- Size: 28.3 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Slug Generator
## Introduction
This package provides an event that will generate a unique slug when saving or creating any Eloquent model.
## Installation
composer require paxha/laravel-sluggable
## Usage
- [Getting Started](#getting-started)
- [Examples](#examples)
### Getting Started
Consider the following table schema for hierarchical data:
```php
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
});
```
Use the `Sluggable` trait in your model to work with slug:
```php
class User extends Model
{
use \Sluggable\Traits\Sluggable;
}
```
By default, the trait expects three things
1. slugFrom(): array (optional) if you using `name` column
2. slugSaveTo(): string (optional) if you using `slug` column
3. separator(): string (optional) default `-`
You can overriding it as well `slugFrom()`, `slugSaveTo()` and `separator()`:
```php
class User extends Model
{
use \Sluggable\Traits\Sluggable;
public static function slugFrom()
{
return ['name']; // or return ['first_name', 'last_name'];
}
public static function slugSaveTo()
{
return 'slug'; // or return ['user_slug'];
}
public static function separator()
{
return '-'; // or return '_';
}
}
```
### Examples
#### Example A
##### Database
```php
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('slug')->unique();
});
```
##### Model
```php
class User extends Model
{
use \Sluggable\Traits\Sluggable;
protected $fillable = [
'first_name', 'last_name',
];
public static function slugFrom()
{
return ['first_name', 'last_name'];
}
}
```
##### Create User
```php
User::create([
'first_name' => 'John',
'last_name' => 'Doe'
]);
// or
$user = new User();
$user->first_name = 'John';
$user->last_name = 'Doe';
$user->save();
```
##### Output
```json5
{
'first_name': 'John',
'last_name': 'Doe',
'slug': 'john-doe',
}
```
#### Example B
##### Table
```php
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('user_slug')->unique();
});
```
##### Model
```php
class User extends Model
{
use \Sluggable\Traits\Sluggable;
protected $fillable = [
'name',
];
public static function slugSaveTo()
{
return ['user_slug'];
}
public static function separator()
{
return '_';
}
}
```
##### Create User
```php
User::create([
'name' => 'John Doe'
]);
// or
$user = new User();
$user->name = 'John Doe';
$user->save();
```
##### Output
```json5
{
'name': 'John Doe',
'user_slug': 'john_doe',
}
```
## License
This is open-sourced laravel library licensed under the [MIT license](https://opensource.org/licenses/MIT).