PHP code example of afzalsabbir / slug-generator

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

    

afzalsabbir / slug-generator example snippets


composer 

    // ...
    use AfzalSabbir\SlugGenerator\SlugGenerator;
    use Illuminate\Database\Eloquent\Model;
    // ...

    class Post extends Model
    {
        // ...
        use SlugGenerator;
        // ...
    }
    

    php artisan vendor:publish --provider="AfzalSabbir\SlugGenerator\SlugGeneratorServiceProvider" --tag="config"
    

    namespace App\Models;
  
    use AfzalSabbir\SlugGenerator\SlugGenerator;
    use Illuminate\Database\Eloquent\Model;
    // ...
    
    class Post extends Model
    {
        // ...
        use SlugGenerator;
    
        // ...
        protected $slugGenerator = [
            "set-on-create" => true, // Whether to set the slug when the model is created
            "set-on-update" => false, // Whether to update the slug when the target field is updated
            "target-field"  => "title", // The field that will be used to generate the slug
            "separator"     => "-", // The separator that will be used to separate the words
            "slug-field"    => "slug", // The field that will be used to store the slug
        ];
  
        // ...
    }
    

    use AfzalSabbir\SlugGenerator\SlugGenerator;
    use App\Models\Post;
    // ...
    
    $post = Post::find(1);
    $post->title = "Hello World";
    $post->slug = generateSlug($post);
    $post->save();