PHP code example of whitecube / laravel-sluggable

1. Go to this page and download the library: Download whitecube/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/ */

    

whitecube / laravel-sluggable example snippets


namespace App;

use Whitecube\Sluggable\HasSlug;

class Post extends Model
{
    use HasSlug;
  
    public $sluggable = 'title';

    public $slugStorageAttribute = 'custom-slug-column';
}

namespace App;

use Whitecube\Sluggable\HasSlug;

class Post extends Model
{
    use HasSlug;
  
    /**
     * Get the attribute name used to generate the slug from
     *
     * @return string
     */
    public function getSluggable()
    {
        return $this->title ? 'title' : 'author';
    }
}

namespace App;

use Whitecube\Sluggable\HasSlug;
use Spatie\Translatable\HasTranslations;

class Post extends Model
{
    use HasSlug;
    use HasTranslations;
  
    public $translatable = ['title', 'slug'];
    
    public $sluggable = 'title';
}

Post::create([
    'title' => [
        'en' => 'The title',
        'fr' => 'Le titre'
    ]
]);

$post->getAttributes()['slug']; // ['en' => 'the-title', 'fr' => 'le-titre']
$post->slug; // the-title (given that the crrent app locale is 'en')
$post->translate('slug', 'fr'); // 'le-titre'

namespace App;

use Whitecube\Sluggable\HasSlug;

class Post extends Model
{
    use HasSlug;
    
    // ...

    protected function getRouteBindingQuery($query) {
        return $query->withTrashed();
    }
}

$post = Post::create([
    'title' => [
        'en' => 'The title in English',
        'fr' => 'Le titre en Français'
    ]
]);

$alternates = [];

foreach (['en', 'fr'] as $locale) {
    $alternates[] = $post->getSluggedUrlForRoute(Route::current(), $locale);
}

/en/posts/the-title
/fr/articles/le-titre

/blog/the-title-in-english/comments
/blog/le-titre-en-francais/comments