PHP code example of parables / laravel-cuid2

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

    

parables / laravel-cuid2 example snippets




namespace App;

use Illuminate\Database\Eloquent\Model;
use Parables\Cuid\GeneratesCuid;

class Post extends Model
{
    use GeneratesCuid;
}

class Post extends Model
{
    public static function cuidColumn(): string
    {
        return 'id';
    }
}



namespace App;

use Parables\Cuid\CuidAsPrimaryKey;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use CuidAsPrimaryKey;

        /**
     * The "type" of the primary key ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

}

// Find a specific post using the `cuidColumn()`
$post = Post::whereCuid($cuid)->first();

// Find multiple posts using the `cuidColumn()`
$post = Post::whereCuid([$first, $second])->get();

// Find a specific post with a custom column name
$post = Post::whereCuid($cuid, 'custom_column')->first();

// Find multiple posts with a custom column name
$post = Post::whereCuid([$first, $second], 'custom_column')->get();

public function getRouteKeyName(): string
{
    return 'cuid';
}

class Post extends Model
{
    public static function cuidColumns(): array
    {
        return [$this->cuidColumn(), 'custom_column'];
    }
}

  public static function cuidColumns(): array
    {
        // Option 1: array of column names: this will use the default size: `24`
        return ['cuid', 'custom_column'];

        // Option 2: array where the key is the column name and the value is the size
        return ['cuid' => 6, 'custom_column' => 10];

    }