PHP code example of eg-mohamed / model-reference

1. Go to this page and download the library: Download eg-mohamed/model-reference 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/ */

    

eg-mohamed / model-reference example snippets


Schema::create('orders', function (Blueprint $table) {
    $table->id();
    $table->string('reference')->unique(); // Add this column to store references
    $table->timestamps();
});

use MoSaid\ModelReference\Traits\HasReference;

class Order extends Model
{
    use HasReference;
}

$order = Order::create([
    'customer_id' => 1,
    'total' => 99.99,
    // No need to specify the reference; it will be generated automatically
]);

echo $order->reference; // Outputs something like "AB12CD"

class Order extends Model
{
    use HasReference;
    
    // Change the column name (default: 'reference')
    protected $referenceColumn = 'order_number';
    
    // Set a prefix (e.g., ORD-123456)
    protected $referencePrefix = 'ORD';
    
    // Set a suffix (e.g., ORD-123456-2023)
    protected $referenceSuffix = '2023';
    
    // Change the length of the random part (default: 6)
    protected $referenceLength = 8;
    
    // Change the separator (default: '-')
    protected $referenceSeparator = '_';
    
    // Change the characters used (default: alphanumeric)
    protected $referenceCharacters = '0123456789';
}

return [
    'column_name' => 'reference',
    'length' => 6,
    'prefix' => '',
    'suffix' => '',
    'separator' => '-',
    'characters' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
];
bash
php artisan vendor:publish --tag="model-reference-config"