PHP code example of artflow-studio / snippets

1. Go to this page and download the library: Download artflow-studio/snippets 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/ */

    

artflow-studio / snippets example snippets


use Livewire\Attributes\On;

class MyComponent extends Component
{
    #[On('afdropdown:selected')]
    public function customerSelected($payload)
    {
        $customerId = $payload['id'];
        $customerData = $payload['data'];
        // Handle selection...
    }
}

@livewire('afdropdown', [
    'model' => 'App\Models\Customer',
    'column' => 'name',
])

@livewire('afdropdown', [
    'model' => 'App\Models\Customer',
    'columns' => ['name', 'email'],
    'searchMode' => 'contains',
    'minSearchLength' => 2,
    'resultLimit' => 15,
    'enableCache' => true,
    'cacheTime' => 3600,
    'placeholder' => 'Search customers...',
    'additionalColumns' => ['email', 'phone'],
    'formatter' => fn($c) => "{$c->name} ({$c->email})",
    'queryCallback' => fn($q) => $q->where('status', 'active'),
])

#[On('afdropdown:selected')]
public function handleSelection($payload)
{
    // $payload['id']     - Model ID
    // $payload['label']  - Display label
    // $payload['data']   - Full model data
    // $payload['model']  - Model class name
}

#[On('afdropdown:cleared')]
public function handleClear()
{
    // Search was cleared
}

@livewire('dynamic-dropdown', [
    'data' => $items,
    'label' => 'name',
    'value' => 'id',
])

use ArtFlowStudio\Snippets\Traits\GeneratesUniqueIds;

class Invoice extends Model
{
    use GeneratesUniqueIds;
    
    protected $uniqueIdColumn = 'invoice_number';
    protected $uniqueIdStrategy = '6digit'; // or 'base36'
}

// Usage
$invoice = Invoice::create([...]);
// invoice_number is auto-generated: "123456" or "ABC123D"

use ArtFlowStudio\Snippets\Traits\FormatsData;

class Customer extends Model
{
    use FormatsData;
}

// Pakistani phone number formatting
Customer::formatPhoneNumber('03001234567'); // +92-300-1234567

// CNIC formatting
Customer::formatCNIC('12345-1234567-1'); // 12345-1234567-1

#[On('afdropdown:selected')]
public function selected($payload) { }

['model' => 'App\Models\Customer', 'column' => 'name']

[
    'model' => 'App\Models\Customer',
    'columns' => ['name', 'email', 'phone'],
    'searchMode' => 'contains',
]

[
    'model' => 'App\Models\Product',
    'column' => 'name',
    'enableCache' => true,
    'cacheTime' => 3600,
]

[
    'model' => 'App\Models\User',
    'column' => 'name',
    'formatter' => fn($u) => "{$u->name} ({$u->email})",
]

// Component
class CreateOrder extends Component
{
    public ?int $customerId = null;

    #[On('afdropdown:selected')]
    public function customerSelected($payload)
    {
        $this->customerId = $payload['id'];
    }

    public function render()
    {
        return view('create-order');
    }
}

// View
@livewire('afdropdown', [
    'model' => 'App\Models\Customer',
    'column' => 'name',
    'placeholder' => 'Search customers...',
])

[
    'id'    => 123,                              // Primary key of selected item
    'label' => 'John Doe',                       // Value of search column
    'class' => 'App\\Models\\Customer',          // Full model class name
    'data'  => [                                 // Complete model data
        'id' => 123,
        'name' => 'John Doe',
        'email' => '[email protected]',
        // ... all model attributes
    ]
]



namespace App\Livewire\BranchManager\Invoices;

use Livewire\Attributes\On;
use Livewire\Component;

class CreateInvoice extends Component
{
    public $customer_id = '';
    public $customer_name = '';
    
    #[On('afdropdown-selected')]
    public function handleCustomerSelected($data)
    {
        // Only process Customer model selections
        if ($data['class'] === 'App\\Models\\Customer') {
            $this->customer_id = $data['id'];
            $this->customer_name = $data['label'];
            
            // You can also access complete model data
            $email = $data['data']['email'] ?? null;
            
            // Emit event to other components if needed
            $this->dispatch('customer-selected', ['id' => $this->customer_id]);
        }
    }
    
    public function render()
    {
        return view('livewire.invoices.create-invoice');
    }
}

#[On('afdropdown-selected')]
public function handleSelection($data)
{
    match ($data['class']) {
        'App\\Models\\Customer' => $this->handleCustomer($data),
        'App\\Models\\Supplier' => $this->handleSupplier($data),
        'App\\Models\\Product' => $this->handleProduct($data),
        default => null
    };
}

private function handleCustomer($data)
{
    $this->customer_id = $data['id'];
}

private function handleSupplier($data)
{
    $this->supplier_id = $data['id'];
}

private function handleProduct($data)
{
    $this->product_id = $data['id'];
}

#[On('afdropdown-cleared')]
public function handleSearchCleared()
{
    $this->customer_id = '';
    $this->customer_name = '';
}



namespace App\Livewire;

use Livewire\Component;

class CreateInvoice extends Component
{
    public $customer_id = '';

    #[On('afdropdown-selected')]
    public function handleCustomerSelected($data)
    {
        if ($data['class'] === 'App\\Models\\Customer') {
            $this->customer_id = $data['id'];
        }
    }

    public function render()
    {
        return view('livewire.invoices.create-invoice');
    }
}

public function getListeners()
{
    return [
        'afdropdown-selected' => 'handleCustomerSelected',
    ];
}

public function handleCustomerSelected($data)
{
    if ($data['class'] === 'App\\Models\\Customer') {
        $this->customer_id = $data['id'];
    }
}



namespace App\Livewire;

use ArtFlowStudio\Snippets\Http\Livewire\AFdropdown;

class CustomCustomerDropdown extends AFdropdown
{
    public function loadResults()
    {
        $this->results = [];
        
        if (strlen($this->search) >= $this->minSearchLength && class_exists($this->model)) {
            try {
                $model = new $this->model;
                $query = $model->where($this->column, 'like', '%' . $this->search . '%')
                    ->where('is_active', true)  // Only active customers
                    ->where('branch_id', auth()->user()->branch_id)  // User's branch only
                    ->whereNull('deleted_at');  // Not deleted
                
                $items = $query->limit($this->resultLimit)->get();
                
                $this->results = $items->map(function($item) {
                    return $this->formatResult($item);
                })->toArray();
            } catch (\Exception $e) {
                $this->results = [];
            }
        }
    }
}

Livewire::component('custom-customer-dropdown', CustomCustomerDropdown::class);

class YourLivewireComponent extends Component
{
    public $city = '';
    
    protected $listeners = ['updateField' => 'setField'];
    
    public function setField($field, $value)
    {
        $this->$field = $value;
    }
}

// Generate unique ID for a model
$uniqueId = generateUniqueID(User::class, 'user_id');

// Generate 6-digit unique ID
$id = unique6digitID(); // Returns: "123456"

// Generate Base36 unique ID
$id = generateUniqueBase36ID(); // Returns: "AB12CD"

// In your model
class User extends Model
{
    protected static function boot()
    {
        parent::boot();
        
        static::creating(function ($model) {
            $model->user_id = generateUniqueID(self::class, 'user_id');
        });
    }
}

// Format Pakistani phone numbers
echo formatContactPK('03001234567');    // +923001234567
echo formatContactPK('00923001234567'); // +923001234567
echo formatContactPK('+923001234567');  // +923001234567
echo formatContactPK('923001234567');   // +923001234567

// International numbers pass through
echo formatContactPK('+12345678901');   // +12345678901

// Format Pakistani CNIC numbers
echo formatCnicPK('1234567890123');     // 12345-6789012-3
echo formatCnicPK('12345-6789012-3');   // 12345-6789012-3

// Handle special cases
echo formatCnicPK('PASSPORT123');       // PASSPORT123 (unchanged)

class User extends Model
{
    // Automatically format phone on save
    public function setPhoneAttribute($value)
    {
        $this->attributes['phone'] = formatContactPK($value);
    }
    
    // Automatically format CNIC on save  
    public function setCnicAttribute($value)
    {
        $this->attributes['cnic'] = formatCnicPK($value);
    }
}
bash
php artisan vendor:publish --provider="ArtFlowStudio\Snippets\SnippetsServiceProvider"
bash
# Already available at: vendor/artflow-studio/snippets/src/Http/Livewire/AFdropdown.php
bash
php artisan vendor:publish --provider="ArtFlowStudio\Snippets\SnippetsServiceProvider"