PHP code example of skaisser / laravel-lead

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

    

skaisser / laravel-lead example snippets


return [
    // The model class that represents leads
    'lead_model' => App\Models\Lead::class,
    
    // Storage layers configuration
    'use_session' => true,
    'use_cookies' => true,
    'cookie_name' => 'lead_id',
    'cookie_duration' => 525600, // 365 days
    
    // Encryption settings
    'encryption_enabled' => true,
    
    // Cache configuration
    'cache_prefix' => 'lead_',
    'cache_ttl' => 1800, // 30 minutes
];

use Skaisser\LaravelLead\Facades\LeadPersistence;

// Store a lead
$lead = Lead::create(['name' => 'John Doe', 'email' => '[email protected]']);
LeadPersistence::storeLead($lead);

// Retrieve current lead (with automatic fallback)
$currentLead = LeadPersistence::getCurrentLead();

// Check if a lead exists
if (LeadPersistence::hasStoredLead()) {
    // Lead exists in storage
}

// Clear stored lead
LeadPersistence::clearStoredLead();

// Save lead data progressively (creates or updates)
$leadData = [
    'name' => request('name'),
    'email' => request('email'),
    'phone' => request('phone'),
    'country_code' => request('country_code'),
    'query_data' => request()->query(), // UTM parameters, etc.
];

$lead = LeadPersistence::saveLead($leadData);

// Update existing lead
$existingLead = LeadPersistence::getCurrentLead();
$updatedLead = LeadPersistence::saveLead($newData, $existingLead);

use Skaisser\LaravelLead\Jobs\ProcessWebhookSubmission;
use Skaisser\LaravelLead\Models\Webhook;

// Create a webhook configuration
$webhook = Webhook::create([
    'name' => 'CRM Integration',
    'url' => 'https://api.crm.com/leads',
    'method' => 'POST',
    'headers' => ['Authorization' => 'Bearer token123'],
    'selected_fields' => ['name', 'email', 'phone', 'company'],
    'field_mapping' => [
        ['original_field' => 'name', 'custom_name' => 'full_name'],
        ['original_field' => 'email', 'custom_name' => 'contact_email'],
    ],
    'timeout' => 30,
    'retry_attempts' => 3,
    'retry_delay' => 60,
]);

// Dispatch webhook for a lead
ProcessWebhookSubmission::dispatch($lead);

use Skaisser\LaravelLead\Services\PhoneFormatter;

$formatter = app(PhoneFormatter::class);

// Format a phone number
$formatted = $formatter->format('11999999999', '+55'); // (11) 99999-9999

// Validate a phone number
$isValid = $formatter->isValid('1234567890', '+1');

// Get international format
$international = $formatter->toInternational('11999999999', '+55'); // +55 11 999999999

// Add custom format for a country
$formatter->addCustomFormat('33', function($numbers) {
    // Custom French formatting
    return preg_replace('/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', '$1 $2 $3 $4 $5', $numbers);
});

// In your Livewire component
$this->dispatch('store-lead', ['leadId' => $encryptedLeadId]);
$this->dispatch('check-local-storage');
$this->dispatch('clear-lead');

// app/Models/Lead.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Lead extends Model
{
    protected $fillable = [
        'name', 'email', 'phone', 'company',
        'country_code', 'query_data', 'accepted_terms'
    ];
    
    protected $casts = [
        'query_data' => 'array',
        'accepted_terms' => 'boolean',
    ];
}

// config/lead-persistence.php
'lead_model' => App\Models\Lead::class,

namespace App\Models;

use Skaisser\LaravelLead\Models\Webhook as BaseWebhook;

class CustomWebhook extends BaseWebhook
{
    public function getAvailableFields(): array
    {
        return array_merge(parent::getAvailableFields(), [
            'custom_field' => 'Custom Field',
            'another_field' => 'Another Field',
        ]);
    }
}

// config/lead-persistence.php
'webhook_transformations' => [
    'phone' => function($value) {
        return '+1' . preg_replace('/[^0-9]/', '', $value);
    },
    'created_at' => function($value) {
        return Carbon::parse($value)->toIso8601String();
    },
],
bash
php artisan vendor:publish --tag=lead-persistence-config
bash
php artisan vendor:publish --tag=lead-persistence-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=lead-persistence-assets