Package Data | |
---|---|
Maintainer Username: | marktopper |
Package Create Date: | 2015-11-27 |
Package Last Update: | 2015-12-17 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-11 15:11:31 |
Package Statistics | |
---|---|
Total Downloads: | 1,689 |
Monthly Downloads: | 49 |
Daily Downloads: | 1 |
Total Stars: | 5 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
Allows your Eloquent Model to automatically generate a unique slug for a attribute on save.
Install using Composer composer require larapack/attribute-slugging 1.*
.
First add the trait Sluggable
to your Eloquent Model.
<?php
namespace App;
use Larapack\AttributeSlugging\Sluggable;
class User
{
use Sluggable;
/**
* @var array List of attribute names which should be slugged
*/
protected $slug = [
'username_slug' => 'username', // set the attribute names you which to slug and from what attribute it should be generated from
];
//...
}
Test:
// we make two user objects
$userA = new App\User;
$userB = new App\User;
// then we set both username's to "Mark"
$userA->username = "Mark";
$userB->username = "Mark";
// save both objects
$userA->save();
$userB->save();
// now the unique slugs have been set
echo $userA->username_slug; // outputs "mark"
echo $userB->username_slug; // outputs "mark-2"