Install Dependencies
composer require milestone/appframe
composer require milestone/interact
(Note: appframe is a prerequisite; ensure it’s properly configured.)
Configure the Package
Add the namespace of your table classes in config/interact.php:
'table_namespace' => 'App\\Interact\\Tables',
Create a Table Class
Define a class matching your database table name (e.g., UserTable) in the configured namespace, implementing _Milestone\Interact\Table:
namespace App\Interact\Tables;
use Milestone\Interact\Table as _Table;
class UserTable implements _Table {
public $table = 'users';
public $mode = 'insert'; // or 'update', 'delete'
public $primary_key = 'id';
public $data = []; // Optional: Default data for all records
}
First Use Case: Import Data
Use the Interact facade to import data from a CSV/array:
use Milestone\Interact\Facades\Interact;
$result = Interact::import('UserTable', $dataArrayOrFilePath);
// Import from an array
$records = [['name' => 'John', 'email' => 'john@example.com']];
Interact::import('UserTable', $records);
// Import from a CSV file
Interact::import('UserTable', 'path/to/file.csv');
$chunkSize = 100;
foreach (array_chunk($records, $chunkSize) as $chunk) {
Interact::import('UserTable', $chunk);
}
$tableClass = 'App\\Interact\\Tables\\ProductTable';
Interact::import($tableClass, $data);
Override isValidImportRecord() to validate each record:
public function isValidImportRecord($record) {
if (empty($record['email'])) {
return 'Email is required';
}
return true; // or false
}
public function preImport($content) {
// Transform data (e.g., normalize arrays)
return array_map('strtolower', $content);
}
public function postImport($content, $result) {
// Log results or trigger events
return $result; // or modified result
}
public function recordImported($record, $executionId) {
// Send email, update related models, etc.
}
Wrap imports in a job to avoid timeouts:
use Milestone\Interact\Facades\Interact;
use Illuminate\Bus\Queueable;
class ImportUsersJob implements ShouldQueue {
use Queueable;
public function handle() {
Interact::import('UserTable', $this->data);
}
}
Trigger events after import:
// In UserTable.php
public function postImport($content, $result) {
event(new UsersImported($result));
}
Extend the package via service providers:
public function register() {
$this->app->bind('interact.table.user', function () {
return new \App\Interact\Tables\UserTable();
});
}
Namespace Mismatch
table_namespace in config/interact.php matches your table classes’ namespace.Primary Key Conflicts
primary_key is misconfigured, updates/inserts may fail silently.DB::table()->where(...)->update() for manual fallbacks.CSV Parsing Issues
League\Csv for complex parsing:
use League\Csv\Reader;
$csv = Reader::createFromPath('file.csv', 'r');
$records = $csv->getRecords();
Memory Limits
Case Sensitivity
$table must match the database exactly (including case).Enable Logging
Add to config/interact.php:
'debug' => env('INTERACT_DEBUG', false),
Logs will appear in storage/logs/interact.log.
Validate SQL Queries Use Laravel’s query logging:
DB::enableQueryLog();
Interact::import('UserTable', $data);
dd(DB::getQueryLog());
Test with Small Datasets Start with 1–2 records to verify hooks/validation before scaling.
Custom Storage Adapters
Override Milestone\Interact\Storage\FileStorage to support S3, databases, etc.
Add New Modes
Extend the mode property to support upsert, truncate, etc.:
public $mode = 'upsert'; // Custom logic in the package
Custom Result Objects
Replace the default Result class by binding your own:
$this->app->bind(
Milestone\Interact\Result::class,
App\Interact\CustomResult::class
);
Add Field Mappings Map CSV headers to database columns dynamically:
public function preImport($content) {
return array_map(function ($record) {
return [
'name' => $record['user_name'], // Map CSV 'user_name' to DB 'name'
'email' => $record['email_address'],
];
}, $content);
}
DB::statement('ALTER TABLE users DISABLE KEYS');
Interact::import('UserTable', $data);
DB::statement('ALTER TABLE users ENABLE KEYS');
insert with chunking for raw performance:
DB::table('users')->insert($chunk);
How can I help you explore Laravel packages today?