Skip to content

Commit ebbb5a3

Browse files
committed
feat: Include connection in seeder when using --database option
When using --database with a non-default connection, the generated seeder now uses DB::connection('name')->table() instead of DB::table() to ensure the seeder targets the correct database. Based on PR #212 by @Endy-c.
1 parent 1143911 commit ebbb5a3

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ php artisan iseed users --clean
104104
```
105105

106106
### database
107-
Optional parameter which specifies the DB connection name.
107+
Optional parameter which specifies the DB connection name. When using a non-default connection, the generated seeder will include the connection specification (e.g., `\DB::connection('mysql2')->table(...)`) to ensure the seeder targets the correct database.
108108

109109
Example:
110110
```

src/Orangehill/Iseed/Iseed.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public function generateSeed($table, $prefix = null, $suffix = null, $database =
108108
$chunkSize,
109109
$prerunEvent,
110110
$postrunEvent,
111-
$indexed
111+
$indexed,
112+
$database
112113
);
113114

114115
// Save a populated stub
@@ -240,22 +241,42 @@ public function getStubPath()
240241
* @param int $chunkSize
241242
* @param string $prerunEvent
242243
* @param string $postrunEvent
244+
* @param bool $indexed
245+
* @param string $database
243246
* @return string
244247
*/
245-
public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true)
248+
public function populateStub($class, $stub, $table, $data, $chunkSize = null, $prerunEvent = null, $postrunEvent = null, $indexed = true, $database = null)
246249
{
247250
$chunkSize = $chunkSize ?: config('iseed::config.chunk_size');
248251

252+
// Determine if we need to specify connection (only for non-default databases)
253+
$defaultDatabase = null;
254+
try {
255+
$defaultDatabase = config('database.default');
256+
} catch (\Exception $e) {
257+
// Config not available (e.g., in tests)
258+
}
259+
$useConnection = $database && $database !== $defaultDatabase;
260+
249261
$inserts = '';
250262
$chunks = array_chunk($data, $chunkSize);
251263
foreach ($chunks as $chunk) {
252264
$this->addNewLines($inserts);
253265
$this->addIndent($inserts, 2);
254-
$inserts .= sprintf(
255-
"\DB::table('%s')->insert(%s);",
256-
$table,
257-
$this->prettifyArray($chunk, $indexed)
258-
);
266+
if ($useConnection) {
267+
$inserts .= sprintf(
268+
"\\DB::connection('%s')->table('%s')->insert(%s);",
269+
$database,
270+
$table,
271+
$this->prettifyArray($chunk, $indexed)
272+
);
273+
} else {
274+
$inserts .= sprintf(
275+
"\\DB::table('%s')->insert(%s);",
276+
$table,
277+
$this->prettifyArray($chunk, $indexed)
278+
);
279+
}
259280
}
260281

261282
$stub = str_replace('{{class}}', $class, $stub);
@@ -281,7 +302,16 @@ public function populateStub($class, $stub, $table, $data, $chunkSize = null, $p
281302
);
282303

283304
if (!is_null($table)) {
284-
$stub = str_replace('{{table}}', $table, $stub);
305+
// Replace the DB::table statement (handles both default and non-default connections)
306+
if ($useConnection) {
307+
$stub = str_replace(
308+
"\\DB::table('{{table}}')",
309+
sprintf("\\DB::connection('%s')->table('%s')", $database, $table),
310+
$stub
311+
);
312+
} else {
313+
$stub = str_replace('{{table}}', $table, $stub);
314+
}
285315
}
286316

287317
$postrunEventInsert = '';

0 commit comments

Comments
 (0)