PHP code example of aram-zahedi / persian-sluggable

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

    

aram-zahedi / persian-sluggable example snippets


$model = new EloquentModel();
$model->name = 'سلام دنیا';
$model->save();

echo $model->slug; // "سلام-دنیا"



namespace App;

use AramZahedi\Sluggable\HasSlug;
use AramZahedi\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;

class YourEloquentModel extends Model
{
    use HasSlug;
    
    /**
     * Get the options for generating the slug.
     */
    public function getSlugOptions() : SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name') // فیلدی که نام از رویش تولید میشود
            ->saveSlugsTo('slug'); // فیلدی که نامک در آن ذخیره میشود
    }
}

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

class CreateYourEloquentModelTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('your_eloquent_models', function (Blueprint $table) {
            $table->increments('id');
            $table->string('slug'); // اسم ستونی که نامک در آن ذخیره میشود
            $table->string('name');
            $table->timestamps();
        });
    }
}


namespace App;

use AramZahedi\Sluggable\HasSlug;
use AramZahedi\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;

class YourEloquentModel extends Model
{
    use HasSlug;
    
    /**
     * Get the options for generating the slug.
     */
    public function getSlugOptions() : SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }
    
    /**
     * Get the route key for the model.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'slug'; // کلید جدید برای router
    }
}

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom(['first_name', 'last_name']) //تولید نامک از ترکیب دو ستون نام و نام خانوادگی
        ->saveSlugsTo('slug');
}

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('slug')
        ->allowDuplicateSlugs();
}

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('slug')
        ->slugsShouldBeNoLongerThan(50);
}

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('slug')
        ->usingSeparator('.');
}

$model = EloquentModel:create(['name' => 'hello world']); // نامک hello-world 
$model->slug = 'my-custom-url';
$model->save(); //نامک جدید "my-custom-url"; 

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('slug')
        ->doNotGenerateSlugsOnCreate();
}

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('slug')
        ->doNotGenerateSlugsOnUpdate();
}

$model = EloquentModel:create(['name' => 'my name']); //نامک "my-name"; 
$model->save();

$model->name = 'new name';
$model->save(); //نامک مثل قبل "my-name"

$model = EloquentModel:create(['name' => 'my name']); //نامک "my-name"; 
$model->save();

$model->name = 'new name';
$model->save(); //نامک مثل قبل "my-name"

$model->generateSlug();
$model->save();//نامک اکنون "new-name"