PHP code example of aesircloud / sluggable

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

    

aesircloud / sluggable example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use AesirCloud\Sluggable\Traits\Sluggable;

class Post extends Model
{
    use Sluggable;

    protected $fillable = ['title', 'slug'];

    /**
     * Optionally override the default slug source field.
     *
     * By default, the package uses whatever is set in 'sluggable.source'
     * or falls back to 'name' on your model. Here, we explicitly use 'title'.
     */
    protected $slugSource = 'title';

    /**
     * If you want to allow the slug to be updated automatically when 'title' changes:
     */
    protected $slugUpdatable = true;

    /**
     * If you want a different slug column in your database table:
     */
    protected $slugColumn = 'url_slug';
}



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            // Make sure it's unique or at least indexed if you rely on uniqueness.
            $table->string('slug')->unique()->after('title');
        });
    }

    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropColumn('slug');
        });
    }
};

return [
    'source'     => 'name',
    'column'     => 'slug',
    'update'     => false,
    'max_length' => 255,
    'scopes'     => [], // e.g. ['category_id'] to scope uniqueness
];


public function getRouteKeyName()
{
    return 'slug';
}

Route::get('/posts/{post:slug}', function (App\Models\Post $post) {
    return $post;
});
bash
  php artisan vendor:publish --provider="AesirCloud\Sluggable\SluggableServiceProvider"