PHP code example of boomdraw / laravel-canonicalizable

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

    

boomdraw / laravel-canonicalizable example snippets


$model = new EloquentModel();
$model->name = 'HeLlO WoRLd';
$model->save();

$model->name_canonical === 'hello world';

// bootstrap/app.php:
$app->withEloquent();
$app->withFacades();

// bootstrap/app.php:
$app->register(Boomdraw\Canonicalizer\CanonicalizerServiceProvider::class);



namespace App;

use Illuminate\Database\Eloquent\Model;
use Boomdraw\Canonicalizable\HasCanonical;
use Boomdraw\Canonicalizable\CanonicalField;
use Boomdraw\Canonicalizable\CanonicalFieldsCollection;

class YourEloquentModel extends Model
{
    use HasCanonical;
    
    /**
     * Get the options for generating the canonical.
     */
    public function getCanonicalFields(): CanonicalFieldsCollection
    {
        return CanonicalFieldsCollection::create()
            ->addField(
                CanonicalField::create()
                    ->from('name')
            )->addField(
                CanonicalField::create()
                    ->from('email')
                    ->type('email')
                    ->to('canonicalized_email')
                    ->doNotGenerateOnUpdate()
            );
    }
}

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('name');
            $table->string('name_canonicalized'); // Field name same as your `to`
            $table->string('email');
            $table->string('canonicalized_email'); // Field name same as your `to`
            $table->timestamps();
        });
    }
}

public function getCanonicalFields(): CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
            CanonicalField::create()
                ->from('name')
                ->disallowDuplicate()
        );
}

public function getCanonicalFields(): CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
            CanonicalField::create()
                ->from('name')
                ->disallowDuplicate('#')
        );
}

public function getCanonicalFields() : CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
            CanonicalField::create()
                ->from('name')
                ->type('other')
        );
}

public function getCanonicalFields() : CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
            CanonicalField::create()
                ->from('name')
                ->callback(function($string) {
                    return mb_strtoupper($string);
                })
        );
}

public function getCanonicalFields() : CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
            CanonicalField::create()
                ->from('name')
                ->type('custom', [$arg1, $arg2])
                ->callback(function($string, $arg, $arg2) {
                    return mb_strtoupper($string.$arg.$arg2);
                })
        )->addField(
             CanonicalField::create()
                 ->from('name')
                 ->type('slug', [$separator])
        );
}

$model = EloquentModel::create(['name' => 'John']); //The canonical is now "john"; 
$model->name_canonical = 'Ivan';
$model->save(); //The canonical is now "Ivan"; 

public function getCanonicalFields() : CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
             CanonicalField::create()
                 ->from('name')
                 ->type('slug')
                 ->forceCanonicalization()
        );
}
...
$model = EloquentModel::create(['name' => 'John']); //The canonical is now "john"; 
$model->name_canonical = 'Test User';
$model->save(); //The canonical is now "test-user"; 

public function getCanonicalFields() : CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
             CanonicalField::create()
                 ->from('name')
                 ->to('name')
                 ->type('slug')
                 ->forceCanonicalization()
        );
}
...
$model = EloquentModel::create(['name' => 'John']); //The is now "john"; 
$model->name = 'Test User';
$model->save(); //The name is now "test-user"; 

public function getCanonicalFields() : CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
            CanonicalField::create()
                ->from('name')
                ->doNotGenerateOnCreate()
        );
}

public function getCanonicalFields() : CanonicalFieldsCollection
{
    return CanonicalFieldsCollection::create()
        ->addField(
            CanonicalField::create()
                ->from('name')
                ->doNotGenerateOnUpdate()
        );
}

$model = EloquentModel::create(['name' => 'John']); //The canonical is now "john"; 
$model->save();

$model->name = 'Ivan';
$model->save(); //The canonical stays "john"