@@ -181,9 +181,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
181181 $ directory .'/.env '
182182 );
183183
184- $ database = $ this ->promptForDatabaseOptions ($ input );
184+ [ $ database, $ migrate ] = $ this ->promptForDatabaseOptions ($ directory , $ input );
185185
186- $ this ->configureDefaultDatabaseConnection ($ directory , $ database , $ name );
186+ $ this ->configureDefaultDatabaseConnection ($ directory , $ database , $ name , $ migrate );
187+
188+ if ($ migrate ) {
189+ $ this ->runCommands ([
190+ $ this ->phpBinary ().' artisan migrate ' ,
191+ ], $ input , $ output , workingPath: $ directory );
192+ }
187193 }
188194
189195 if ($ input ->getOption ('git ' ) || $ input ->getOption ('github ' ) !== false ) {
@@ -231,47 +237,42 @@ protected function defaultBranch()
231237 * @param string $directory
232238 * @param string $database
233239 * @param string $name
240+ * @param bool $migrate
234241 * @return void
235242 */
236- protected function configureDefaultDatabaseConnection (string $ directory , string $ database , string $ name )
243+ protected function configureDefaultDatabaseConnection (string $ directory , string $ database , string $ name, bool $ migrate )
237244 {
238245 // MariaDB configuration only exists as of Laravel 11...
239246 if ($ database === 'mariadb ' && ! $ this ->hasMariaDBConfig ($ directory )) {
240247 $ database = 'mysql ' ;
241248 }
242249
243- $ this ->replaceInFile (
244- 'DB_CONNECTION=mysql ' ,
250+ $ this ->pregReplaceInFile (
251+ '/ DB_CONNECTION=.*/ ' ,
245252 'DB_CONNECTION= ' .$ database ,
246253 $ directory .'/.env '
247254 );
248255
249- if (! in_array ($ database , ['sqlite ' ])) {
250- $ this ->replaceInFile (
251- 'DB_CONNECTION=mysql ' ,
252- 'DB_CONNECTION= ' .$ database ,
253- $ directory .'/.env.example '
254- );
255- }
256-
257- $ defaults = [
258- 'DB_HOST=127.0.0.1 ' ,
259- 'DB_PORT=3306 ' ,
260- 'DB_DATABASE=laravel ' ,
261- 'DB_USERNAME=root ' ,
262- 'DB_PASSWORD= ' ,
263- ];
256+ $ this ->pregReplaceInFile (
257+ '/DB_CONNECTION=.*/ ' ,
258+ 'DB_CONNECTION= ' .$ database ,
259+ $ directory .'/.env.example '
260+ );
264261
265262 if ($ database === 'sqlite ' ) {
266- $ this ->replaceInFile (
267- $ defaults ,
268- collect ($ defaults )->map (fn ($ default ) => "# {$ default }" )->all (),
269- $ directory .'/.env '
270- );
263+ $ environment = file_get_contents ($ directory .'/.env ' );
264+
265+ // If database options aren't commented, comment them for SQLite...
266+ if (! str_contains ($ environment , '# DB_HOST=127.0.0.1 ' )) {
267+ return $ this ->commentDatabaseConfigurationForSqlite ($ directory );
268+ }
271269
272270 return ;
273271 }
274272
273+ // Any commented database configuration options should be uncommented when not on SQLite...
274+ $ this ->uncommentDatabaseConfiguration ($ directory );
275+
275276 $ defaultPorts = [
276277 'pgsql ' => '5432 ' ,
277278 'sqlsrv ' => '1433 ' ,
@@ -323,6 +324,64 @@ protected function hasMariaDBConfig(string $directory): bool
323324 );
324325 }
325326
327+ /**
328+ * Comment the irrelevant database configuration entries for SQLite applications.
329+ *
330+ * @param string $directory
331+ * @return void
332+ */
333+ protected function commentDatabaseConfigurationForSqlite (string $ directory )
334+ {
335+ $ defaults = [
336+ 'DB_HOST=127.0.0.1 ' ,
337+ 'DB_PORT=3306 ' ,
338+ 'DB_DATABASE=laravel ' ,
339+ 'DB_USERNAME=root ' ,
340+ 'DB_PASSWORD= ' ,
341+ ];
342+
343+ $ this ->replaceInFile (
344+ $ defaults ,
345+ collect ($ defaults )->map (fn ($ default ) => "# {$ default }" )->all (),
346+ $ directory .'/.env '
347+ );
348+
349+ $ this ->replaceInFile (
350+ $ defaults ,
351+ collect ($ defaults )->map (fn ($ default ) => "# {$ default }" )->all (),
352+ $ directory .'/.env.example '
353+ );
354+ }
355+
356+ /**
357+ * Uncomment the relevant database configuration entries for non SQLite applications.
358+ *
359+ * @param string $directory
360+ * @return void
361+ */
362+ protected function uncommentDatabaseConfiguration (string $ directory )
363+ {
364+ $ defaults = [
365+ '# DB_HOST=127.0.0.1 ' ,
366+ '# DB_PORT=3306 ' ,
367+ '# DB_DATABASE=laravel ' ,
368+ '# DB_USERNAME=root ' ,
369+ '# DB_PASSWORD= ' ,
370+ ];
371+
372+ $ this ->replaceInFile (
373+ $ defaults ,
374+ collect ($ defaults )->map (fn ($ default ) => substr ($ default , 2 ))->all (),
375+ $ directory .'/.env '
376+ );
377+
378+ $ this ->replaceInFile (
379+ $ defaults ,
380+ collect ($ defaults )->map (fn ($ default ) => substr ($ default , 2 ))->all (),
381+ $ directory .'/.env.example '
382+ );
383+ }
384+
326385 /**
327386 * Install Laravel Breeze into the application.
328387 *
@@ -382,12 +441,14 @@ protected function installJetstream(string $directory, InputInterface $input, Ou
382441 /**
383442 * Determine the default database connection.
384443 *
444+ * @param string $directory
385445 * @param \Symfony\Component\Console\Input\InputInterface $input
386446 * @return string
387447 */
388- protected function promptForDatabaseOptions (InputInterface $ input )
448+ protected function promptForDatabaseOptions (string $ directory , InputInterface $ input )
389449 {
390- $ database = 'mysql ' ;
450+ // Laravel 11.x appliations use SQLite as default...
451+ $ defaultDatabase = $ this ->hasMariaDBConfig ($ directory ) ? 'sqlite ' : 'mysql ' ;
391452
392453 if ($ input ->isInteractive ()) {
393454 $ database = select (
@@ -399,11 +460,15 @@ protected function promptForDatabaseOptions(InputInterface $input)
399460 'sqlite ' => 'SQLite ' ,
400461 'sqlsrv ' => 'SQL Server ' ,
401462 ],
402- default: $ database
463+ default: $ defaultDatabase
403464 );
465+
466+ if ($ database !== $ defaultDatabase ) {
467+ $ migrate = confirm (label: 'Default database updated. Would you like to run the default database migrations? ' , default: true );
468+ }
404469 }
405470
406- return $ database ;
471+ return [ $ database ?? $ defaultDatabase , $ migrate ?? false ] ;
407472 }
408473
409474 /**
@@ -808,4 +873,20 @@ protected function replaceInFile(string|array $search, string|array $replace, st
808873 str_replace ($ search , $ replace , file_get_contents ($ file ))
809874 );
810875 }
876+
877+ /**
878+ * Replace the given string in the given file using regular expressions.
879+ *
880+ * @param string|array $search
881+ * @param string|array $replace
882+ * @param string $file
883+ * @return void
884+ */
885+ protected function pregReplaceInFile (string $ pattern , string $ replace , string $ file )
886+ {
887+ file_put_contents (
888+ $ file ,
889+ preg_replace ($ pattern , $ replace , file_get_contents ($ file ))
890+ );
891+ }
811892}
0 commit comments