Install the Package:
composer require rasel9w9/data-generator:^1.2
Ensure PDO_OCI is enabled in your PHP environment if using Oracle 18c.
Register the Service Provider (Laravel <5.5):
Add to config/app.php:
'providers' => [
rasel9w9\DataGenerator\DataGeneratorServiceProvider::class,
],
First Use Case: Export a specific table via Artisan:
php artisan alauddin:generate-table-data --table=users
Output: PHP array file saved to database/seeders/data/users (no extension).
Artisan Command Usage:
php artisan alauddin:generate-table-data --table=posts
php artisan alauddin:generate-data
Controller Integration:
use rasel9w9\DataGenerator\GenerateData;
class DataController extends Controller {
public function exportTable($tableName) {
$generator = new GenerateData();
$generator->generateTableData($tableName);
return response()->json(['status' => 'Exported to seeders/data']);
}
}
Multi-Database Handling:
config/database.php includes an Oracle connection:
'oracle' => [
'driver' => 'oracle',
'host' => env('ORACLE_HOST'),
'database' => env('ORACLE_DB'),
'username' => env('ORACLE_USER'),
'password' => env('ORACLE_PASSWORD'),
'prefix' => '',
'charset' => 'utf8',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
$generator->generateTableData('users', 'oracle');
Automated Scheduling:
Add to app/Console/Kernel.php:
protected function schedule(Schedule $schedule) {
$schedule->command('alauddin:generate-data')->dailyAt('2:00');
}
if (!Schema::hasTable($tableName)) {
throw new \InvalidArgumentException("Table {$tableName} does not exist.");
}
database/seeders/data by extending the GenerateData class:
class CustomGenerator extends GenerateData {
protected $outputPath = 'custom/path/';
}
$data = $generator->generateTableData($tableName);
array_walk($data, function(&$row) {
$row['password'] = '*****'; // Example: Mask sensitive fields
});
file_put_contents("{$this->outputPath}{$tableName}", '<?php return ' . var_export($data, true) . ';');
Oracle-Specific Quirks:
CLOB or TIMESTAMP WITH TIME ZONE may not map cleanly to Laravel’s default types. Handle with:
$generator->setTypeMapper([
'oracle.clob' => 'string',
'oracle.timestamp' => 'datetime',
]);
PDO_OCI extension is missing. Verify with:
php -m | grep pdo_oci
Performance Bottlenecks:
$generator->setChunkSize(5000); // Process 5K rows at a time
File Overwrites: The package overwrites files by default. Mitigate by:
$filename = "{$tableName}_" . now()->format('YmdHis');
file_exists checks before writing.Laravel Version Conflicts:
composer.json dependencies.Enable Query Logging:
Add to config/database.php:
'log' => true,
'log_queries' => true,
Check storage/logs/laravel.log for failed queries.
Validate Output: Compare row counts between source and exported data:
$sourceCount = DB::table($tableName)->count();
$exportedCount = count($generator->generateTableData($tableName));
if ($sourceCount !== $exportedCount) {
throw new \RuntimeException("Row count mismatch for {$tableName}!");
}
Oracle-Specific Errors:
SELECT on the table:
GRANT SELECT ON your_table TO your_laravel_user;
Custom Exporters:
Extend the GenerateData class to support additional databases or formats (e.g., CSV):
class CsvGenerator extends GenerateData {
public function generateCsv($tableName, $filePath) {
$data = $this->fetchTableData($tableName);
$csv = fopen($filePath, 'w');
foreach ($data as $row) {
fputcsv($csv, $row);
}
fclose($csv);
}
}
Pre/Post-Export Hooks: Override methods to add logic:
class HookedGenerator extends GenerateData {
protected function beforeExport($tableName) {
// Example: Log start time
Log::info("Exporting {$tableName} at " . now());
}
protected function afterExport($tableName, $data) {
// Example: Send notification
Mail::to('admin@example.com')->send(new ExportComplete($tableName));
}
}
Dynamic Table Selection: Filter tables by prefix or regex:
public function generateFilteredData($prefix = '') {
$tables = Schema::getAllTables();
$filtered = array_filter($tables, fn($table) => str_starts_with($table, $prefix));
foreach ($filtered as $table) {
$this->generateTableData($table);
}
}
Connection Management: Dynamically switch connections:
public function generateTableData($tableName, $connection = null) {
$this->connection = $connection ?? config('database.default');
// ... rest of the logic
}
Default Connection: The package defaults to Laravel’s default database connection. Explicitly pass the connection name if needed (check future updates for support).
Output Directory Permissions:
Ensure database/seeders/data is writable:
mkdir -p database/seeders/data
chmod -R 775 database/seeders/data
PHP Memory Limits:
Increase memory_limit in php.ini for large exports:
memory_limit = 512M
Or set it dynamically:
ini_set('memory_limit', '512M');
Time Zone Handling: Oracle timestamps may differ from Laravel’s default time zone. Normalize with:
$data = array_map(function($row) {
return array_map('DateTime::createFromFormat', $row, array_fill(0, count($row), 'Y-m-d H:i:s'));
}, $data);
How can I help you explore Laravel packages today?