mgboateng / eloquent-slug by mgboateng

Create elegant slugs in Laravel models with ease
332
3
0
Package Data
Maintainer Username: mgboateng
Maintainer Contact: mgyan84@gmail.com (Michael G. Boateng)
Package Create Date: 2017-06-27
Package Last Update: 2018-05-20
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-12-23 03:08:25
Package Statistics
Total Downloads: 332
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 3
Total Watchers: 0
Total Forks: 0
Total Open Issues: 1

The package provides a trait that allows you to save a unique slugs to your database seamlessly by just specifying the seperator, source and destination field to generate a slug. It is very fast and very light on resources as it makes just a single database call when creating a model and two when updating as opposed to looping recurcively over the database to generate a unique slug.

Installation

You can install the package through composer composer require mgboateng/eloquent-slugs or through your composer json file:

{
    "require": {
        "mgboateng/eloquent-slugs" : "~0.2"          
    }
}

Usage

To use the package you simply add the Slugging trait to you model and set protected $slugSettings property as below:

<?php 
namespace App;

use Illuminate\Database\Eloquent\Model;
use MGBoateng\EloquentSlugs\Slugging;

class Post extends Model 
{
    use Slugging;

    protected $slugSettings = [
        'source' => 'title',
        'destination' => 'slug',
        'seperator' => '-'
    ];    
}

The protected $slugSettings array sets

  • The source key which indicate the field to be used as source for making slugs
  • The destination key which specifies the field to store generated slugs
  • The seperator key specifies the seperator to use when generating slugs. eg. '-', '_'

When you are creating a model with a settings as:

protected $slugSettings = [
        'source' => 'title',
        'destination' => 'slug',
        'seperator' => '-'
    ];    

when you craete a model

Post::create([
    'title' => 'Hello World',
    'body' => 'Here comes a great programmer'
]);

an output of

[
    'title' => 'Hello World',
    'slug' => 'hello-world', // or 'hello-world-1' if hello-world already exist
    'body' => 'Here comes a great programmer'
]

will be generated.

You could set the destination field (slug in the above example) to generate a unique slug that is different from the source (title in the above example). When the destination field is directly set it takes precedent over the source field as the source for generating slugs. eg.

Post::create([
    'title' => 'Hello World',
    'slug' => 'Welcome Home'
    'body' => 'Here comes a great programmer'
]);

will output:

[
    'title' => 'Hello World',
    'slug' => 'welcome-home', // or welcome-home-1 if welcome-home already exist
    'body' => 'Here comes a great programmer'
]

License

This software is distributed under the MIT license.