PHP code example of yieldstudio / eloquent-public-id

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

    

yieldstudio / eloquent-public-id example snippets


Schema::create('users', function (Blueprint $table) {
    // ..
    $table->uuid('public_id')->index()->unique();
    // ..
});



declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use YieldStudio\EloquentPublicId\HasPublicId;

class User extends Model
{
    use HasPublicId;
}



declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use YieldStudio\EloquentPublicId\HasPublicId;

class User extends Model
{
    use HasPublicId;
    
    public function getPublicIdName(): string
    {
        return 'uuid';
    }
}



declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use YieldStudio\EloquentPublicId\HasPublicId;

class User extends Model
{
    use HasPublicId;
    
    public function generatePublicId(): string
    {
        return Str::random();
    }
}



declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use YieldStudio\EloquentPublicId\ConvertPublicId;

class CreatePostRequest extends FormRequest
{
    use ConvertPublicId;

    protected array $publicIdsToConvert = [
        'category_id' => Category::class,
        'tags.*' => Tag::class,
        'postable_id' => 'postable_type', // You can reference another field as model class in case of morph relationship
        'suggestions' => [ // Nesting fields is allowed
            '*' => [
                'post_id' => Post::class,
                'tags.*' => Tag::class,
                'postable_id' => 'postable_type',
            ]
        ]
    ];