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/ */

    

rolies106 / eloquent-sluggable example snippets


'providers' => [
    // ...
    'Rolies106\EloquentSluggable\SluggableServiceProvider',
];

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();

$new_post = $post->replicate()->resluggify();

$post = Post::findBySlug('my-slug');

return [
	'build_from'      => null,
	'save_to'         => 'slug',
	'max_length'      => null,
	'method'          => null,
	'separator'       => '-',
	'unique'          => true,
	'

class Person extends Eloquent implements SluggableInterface
{
	use SluggableTrait;

	protected $sluggable = [
		'build_from' => 'fullname',
	]

	public function getFullnameAttribute() {
		return $this->firstname . ' ' . $this->lastname;
	}
}

Schema::create('posts', function ($table) {
	$table->increments('id');
	$table->string('title');
	$table->string('body');
	$table->string('slug');
	$table->timestamps();
});

	'method' => ['Illuminate\\Support\\Str', 'slug'],

	'method' => function ($string, $separator) {
		return strtolower(preg_replace('/[^a-z]+/i', $separator, $string));
	},

    $app->singleton(
        'router',
        '\Rolies106\EloquentSluggable\SluggableRouter'
    );

    Post::findBySlugOrId('slug-or-id');

    Post::findBySlugOrIdOrFail('slug-or-id');

php artisan sluggable:table posts

php artisan sluggable:table posts slug_column