PHP code example of brainstud / has-identifier

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

    

brainstud / has-identifier example snippets


/**
 * @property string $identifier
 */
class Item extends Model
{
    use HasIdentifier;
    
    protected $fillable = [
        'identifier'
    ];
}

$model = Item::create();
echo $model->identifier;

/**
 * @property string $identifier
 */
class Item extends Model
{
    use HasIdentifier;
    
    protected string $identifierAttribute = 'uuid';
    
    protected $fillable = [
        'uuid'
    ];
}

// Find method
$model = Item::findByIdentifier($uuid);
$model = Item::findOrFailByIdentifier($uuid);

// Scope
$model = Item::identifiedBy($uuid)->first();