Giter Site home page Giter Site logo

mattglover11 / laravel-sluggable-trait Goto Github PK

View Code? Open in Web Editor NEW

This project forked from martinbean/laravel-sluggable-trait

0.0 1.0 0.0 6 KB

A trait you can apply to Eloquent models to have slugs automatically generated on save.

License: MIT License

PHP 100.00%

laravel-sluggable-trait's Introduction

Laravel Sluggable Trait

A trait you can apply to Eloquent models to have slugs automatically generated on save.


Installation

$ composer require martinbean/laravel-sluggable-trait

## Usage
<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use MartinBean\Database\Elouqent\Sluggable;

class Item extends Model {

	use Sluggable;

}

By default, the trait assumes your database has two columns: name and slug. If you need to change these, you can do so via class constants:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use MartinBean\Database\Elouqent\Sluggable;

class Item extends Model {

	use Sluggable;

	const SLUGGABLE_SLUG_COLUMN = 'headline';
	const SLUGGABLE_NAME_COLUMN = 'seo_url';

}

### Regenerating Slugs ### By default, the trait will not overwrite slugs if there is already a value in the model's slug column. To override, this feature, the slug column should be set to null prior to calling the model's save function. E.g.
$mymodel->myslugcolumn = '';
$mymodel->save();

### Unique Slugs ###

The trait supports the generation of unique traits by adding a hexidecimal counter to the end of the slug name if the slug already exists. The counter can also be prefixed with a static OR dynamically generated value. To use a static prefix on the counter, in your model use:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use MartinBean\Database\Elouqent\Sluggable;

class Item extends Model {

	use Sluggable;

	const SLUGGABLE_SLUG_COLUMN = 'headline';
	const SLUGGABLE_NAME_COLUMN = 'seo_url';
	const SLUGGABLE_UNIQUE_PREFIXER = 'news';

}

To generate a runtime prefix to prepend to the slug counter when generating a unique slug, you may define a function sluggableUniquePrefixer in your model code that may return a value, based on your model's values. For example:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use MartinBean\Database\Elouqent\Sluggable;

class Item extends Model {

	use Sluggable;

	const SLUGGABLE_SLUG_COLUMN = 'headline';
	const SLUGGABLE_NAME_COLUMN = 'seo_url';
	// SLUGGABLE_UNIQUE_PREFIXER will be ignored if a sluggableUniquePrefixer method is present
	const SLUGGABLE_UNIQUE_PREFIXER = 'news';  


    public function sluggableUniquePrefixer() {
        if ($this->author)
            return substr($this->author->firstName,0,1).substr($this->author->lastName,0,1);
        return '';
    }

}

License

Licensed under the MIT Licence.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.