PHP code example of abdulrhmansouda / muid

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

    

abdulrhmansouda / muid example snippets


    public function up(): void
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->muid()->primary(); \\ by default the column name is "muid"
            $table->timestamps();
        });
    }

use Illuminate\Database\Eloquent\Model;
use MUID\HasMUID;

class TableName extends Model
{
    use HasMUID;
}

    public function up(): void
    {
        Schema::create('table_name', function (Blueprint $table) {
            $table->muid('id')->primary();
            $table->muid('unique_code', 5)->unique();
            $table->timestamps();
        });
    }

use Illuminate\Database\Eloquent\Model;
use MUID\HasMUID;

class TableName extends Model
{
    use HasMUID;

    protected static function get_muid_columns(): array
    {
        return [
            [
                'column_name'   => 'id',
                // 'length'    => 10, default length is 10
                // 'charset'   => '0123456789abcdefghijklmnopqrstuvwxyz', default chareset
            ],
            [
                'column_name'   => 'unique_code',
                'length'    => 5,
                'charset'   => '0123456789',
            ],
        ];
    }
}

use Illuminate\Support\Str;

$unique_muid = Str::generateMUIDByModel(ModelName::class); // default column name is muid.

$unique_muid = Str::generateMUIDByModel(ModelName::class, 'column_name');

use Illuminate\Support\Str;

$unique_muid = Str::generateMUIDByTable('table_name');
// default column_name = 'muid'
// default column_length = 10
// default charset = '0123456789abcdefghijklmnopqrstuvwxyz'

$unique_muid = Str::generateMUIDByTable('table_name', 'column_name', 5, '0123456789');

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('table_name', function (Blueprint $table) {
            $table->muid('new_column_name')->nullable();
        });

        TableName::all()->each(function ($model_instance) {
            $model_instance->generateMUID(['new_column_name']);
            $model_instance->save();
        });

        Schema::table('table_name', function (Blueprint $table) {
            $table->muid('new_column_name')
                ->nullable(false)
                ->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('table_name', function (Blueprint $table) {
            $table->dropColumn(['new_column_name']);
        });
    }
};
bash
php artisan vendor:publish --tag=muid-config