1. Go to this page and download the library: Download padosoft/laravel-sluggable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$model = new EloquentModel();
$model->name = 'activerecord is awesome';
$model->save();
echo $model->slug; // outputs "activerecord-is-awesome"
namespace App;
use Padosoft\Sluggable\HasSlug;
use Illuminate\Database\Eloquent\Model;
class YourEloquentModel extends Model
{
use HasSlug;
}
namespace App;
use Padosoft\Sluggable\HasSlug;
use Padosoft\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;
class YourEloquentModel extends Model
{
use HasSlug;
/**
* Get the options for generating the slug.
*/
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('slug');
}
}
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom(['first_name', 'last_name'])
->saveSlugsTo('slug');
}
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('category.name')
->saveSlugsTo('slug');
}
public function category()
{
return $this->belongsTo('\App\Category', 'category_id');
}
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->slugifyUseLanguage('jp')
->saveSlugsTo('slug');
}
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->slugifyUseDictionary(['@'=>'at','['=>'quad'])
->saveSlugsTo('slug');
}
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('url')
->allowDuplicateSlugs()
->disallowSlugIfAllSourceFieldsEmpty()
;
}
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('url')
->slugsShouldBeNoLongerThan(50)
->randomSlugsShouldBeNoLongerThan(20);
}
$model = EloquentModel:create(['name' => 'my name']); //url is now "my-name";
$model->url = 'my-custom-url';
$model-save();
$model->name = 'changed name';
$model->save(); //url stays "my name"
//if you reset the slug and recall save it will regenerate the slug.
$model->url = '';
$model-save(); //url is now "changed-name";
->saveCustomSlugsTo('url-custom')
$model = new class extends TestModel
{
public function getSlugOptions(): SlugOptions
{
return parent::getSlugOptions()->generateSlugsFrom('name')
->saveCustomSlugsTo('url_custom');
}
};
$model->name = 'hello dad';
$model->url_custom = 'this is a custom test';
$model->save(); //the slug is 'this-is-a-custom-test' and not , 'hello-dad';