PHP code example of corazzi / sluggable

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

    

corazzi / sluggable example snippets


namespace Acme\Models;

use Illuminate\Database\Eloquent\Model;
use Corazzi\Sluggable\Sluggable;

class Post extends Model
{
    use Sluggable;
}

$post = Post::create([
    'name' => 'My first post'
]);

$post->slug; // my-first-post

namespace Acme\Models;

use Illuminate\Database\Eloquent\Model;
use Corazzi\Sluggable\Sluggable;

class Post extends Model
{
    use Sluggable;
    
    protected $slugOrigin = 'title';
    
    protected $slugColumn = 'post_name';
}

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post

// my-second-post already exists

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post-1

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post-2

// ...and so on

$post = Post::create([
    'name' => 'How to explicitly set a slug',
    'slug' => 'explicitly-setting-slugs-with-sluggable'
]);

$post->slug; // explicitly-setting-slugs-with-sluggable