PHP code example of rolies106 / eloquent-sluggable
1. Go to this page and download the library: Download rolies106/eloquent-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/ */
use Rolies106\EloquentSluggable\SluggableInterface;
use Rolies106\EloquentSluggable\SluggableTrait;
class Post extends Model implements SluggableInterface
{
use SluggableTrait;
protected $sluggable = [
'build_from' => 'title',
'save_to' => 'slug',
'generate_path' => true, // Generate path to save to table
'path_with_domain' => false, // Include domain in path
'is_tree' => true, // Generate path for tree e.g categories
'parent_relation' => 'parent', // Function for relation to parent
'path_column' => 'path' // Column where path will be saved
];
}
$post = new Post([
'title' => 'My Awesome Blog Post',
]);
$post->save();
echo $post->slug;
// or, if you don't know the name of the slug attribute:
echo $post->getSlug();