1. Go to this page and download the library: Download intrfce/laravel-public-ids 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/ */
intrfce / laravel-public-ids example snippets
$user->id; // "0192f8a1-9b2c-71d4-a716-446655440000" (raw UUID — the ORM key)
$user->public_id; // "user_3kQ4mZp8Vh7kQp2Rt5Nx9" (what the world sees)
User::find('user_3kQ4mZp8Vh7kQp2Rt5Nx9'); // decodes, then finds
route('users.show', $user); // /users/user_3kQ4mZp8Vh7kQp2Rt5Nx9
use Illuminate\Database\Eloquent\Model;
use Intrfce\PublicIds\Concerns\HasPublicIds;
class User extends Model
{
use HasPublicIds;
public function idPrefix(): string
{
return 'user';
}
}
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
// ...
});
$user = User::create(['name' => 'Ada']);
$user->id; // raw UUID — used by relationships, foreign keys, joins
$user->getKey(); // same raw UUID
$user->public_id; // "user_3kQ4mZp8Vh7kQp2Rt5Nx9"