Skip to content

Commit f2db5b2

Browse files
committed
feat: update docs for multiple dbs on mvc
1 parent 386421f commit f2db5b2

File tree

2 files changed

+197
-3
lines changed

2 files changed

+197
-3
lines changed

src/docs/database/files.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ This example will add a `created_at` column to the `posts` table with the curren
137137

138138
## Multiple DB connections <Badge>New</Badge>
139139

140-
Leaf MVC supports multiple database connections via the `database.php` configuration file. By default, Leaf uses the `default` connection for all database operations. However, if you want to use a different connection for a specific table, you can specify the connection in the schema file using the `connection` key. Here's an example:
140+
Leaf MVC supports multiple database connections via the `config/database.php` configuration file. By default, Leaf uses the `default` connection for all database operations. However, if you want to use a different connection for a specific table, you can specify the connection in the schema file using the `connection` key. Here's an example:
141141

142142
```yml:no-line-numbers [posts.yml]
143143
connection: postsDbConnection
@@ -147,6 +147,103 @@ columns:
147147
body: text
148148
```
149149

150+
::: details Example Multi-Database Setup
151+
152+
Here is an example of how you might set up multiple database connections in your `config/database.php` file:
153+
154+
```php
155+
<?php
156+
157+
return [
158+
159+
/*
160+
|--------------------------------------------------------------------------
161+
| Default Database Connection Name
162+
|--------------------------------------------------------------------------
163+
|
164+
| Here you may specify which of the database connections below you wish
165+
| to use as your default connection for all database work. Of course
166+
| you may use many connections at once using the Database library.
167+
|
168+
*/
169+
'default' => _env('DB_CONNECTION', 'pgsql'),
170+
171+
/*
172+
|--------------------------------------------------------------------------
173+
| Database Connections
174+
|--------------------------------------------------------------------------
175+
|
176+
| Here are each of the database connections setup for your application.
177+
| Of course, examples of configuring each database platform that is
178+
| supported by eloquent is shown below to make development simple.
179+
|
180+
|
181+
| All database work in eloquent is done through the PHP PDO facilities
182+
| so make sure you have the driver for your particular database of
183+
| choice installed on your machine before you begin development.
184+
|
185+
*/
186+
'connections' => [
187+
'sqlite' => [
188+
'driver' => 'sqlite',
189+
'url' => _env('DATABASE_URL'),
190+
'database' => _env('DB_DATABASE', AppPaths('databaseStorage') . '/database.sqlite'),
191+
'prefix' => '',
192+
'foreign_key_constraints' => _env('DB_FOREIGN_KEYS', true),
193+
],
194+
195+
'pgsql' => [
196+
'driver' => 'pgsql',
197+
'url' => _env('DATABASE_URL'),
198+
'host' => _env('DB_HOST', '127.0.0.1'),
199+
'port' => _env('DB_PORT', '5432'),
200+
'database' => _env('DB_DATABASE', 'forge'),
201+
'username' => _env('DB_USERNAME', 'forge'),
202+
'password' => _env('DB_PASSWORD', ''),
203+
'charset' => _env('DB_CHARSET', 'utf8'),
204+
'prefix' => '',
205+
'prefix_indexes' => true,
206+
'schema' => 'public',
207+
'sslmode' => 'prefer',
208+
],
209+
210+
'analytics' => [
211+
'driver' => 'pgsql',
212+
'url' => _env('ANALYTICS_DATABASE_URL'),
213+
'host' => _env('ANALYTICS_DB_HOST', '127.0.0.1'),
214+
'port' => _env('ANALYTICS_DB_PORT', '5432'),
215+
'database' => _env('ANALYTICS_DB_DATABASE', 'forge'),
216+
'username' => _env('ANALYTICS_DB_USERNAME', 'forge'),
217+
'password' => _env('ANALYTICS_DB_PASSWORD', ''),
218+
'charset' => _env('ANALYTICS_DB_CHARSET', 'utf8'),
219+
'prefix' => '',
220+
'prefix_indexes' => true,
221+
'schema' => 'public',
222+
'sslmode' => 'prefer',
223+
],
224+
225+
'imports' => [
226+
'driver' => 'pgsql',
227+
'url' => _env('IMPORTS_DATABASE_URL'),
228+
'host' => _env('IMPORTS_DB_HOST', '127.0.0.1'),
229+
'port' => _env('IMPORTS_DB_PORT', '5432'),
230+
'database' => _env('IMPORTS_DB_DATABASE', 'forge'),
231+
'username' => _env('IMPORTS_DB_USERNAME', 'forge'),
232+
'password' => _env('IMPORTS_DB_PASSWORD', ''),
233+
'charset' => _env('IMPORTS_DB_CHARSET', 'utf8'),
234+
'prefix' => '',
235+
'prefix_indexes' => true,
236+
'schema' => 'public',
237+
'sslmode' => 'prefer',
238+
],
239+
],
240+
];
241+
```
242+
243+
This example defines three PostgreSQL connections: `pgsql`, `analytics`, and `imports`, and an SQLite connection. You can then specify which database you want to run a migration on by setting the `connection` property as shown above.
244+
245+
:::
246+
150247
## Schema columns
151248

152249
In a schema file, you can define the columns of your table under the `columns` key. The key is the column name and the value is the type of column or an array of properties for the column:

src/docs/database/models.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ class Flight extends Model
256256
}
257257
```
258258

259-
## Database Connection
259+
## Multiple Database Connections
260260

261-
By default, all Leaf models will use the default database connection configured for your application. If you would like to specify a different connection for the model, use the `$connection` property:
261+
By default, all Leaf models will use the default database connection configured for your application. If you would like to specify a different database connection for the model, you need to set that up first in your `config/database.php` configuration file. Once you have done that, you may specify the connection name in your model like this:
262262

263263
```php
264264
class Flight extends Model
@@ -272,6 +272,103 @@ class Flight extends Model
272272
}
273273
```
274274

275+
::: details Example Multi-Database Setup
276+
277+
Here is an example of how you might set up multiple database connections in your `config/database.php` file:
278+
279+
```php
280+
<?php
281+
282+
return [
283+
284+
/*
285+
|--------------------------------------------------------------------------
286+
| Default Database Connection Name
287+
|--------------------------------------------------------------------------
288+
|
289+
| Here you may specify which of the database connections below you wish
290+
| to use as your default connection for all database work. Of course
291+
| you may use many connections at once using the Database library.
292+
|
293+
*/
294+
'default' => _env('DB_CONNECTION', 'pgsql'),
295+
296+
/*
297+
|--------------------------------------------------------------------------
298+
| Database Connections
299+
|--------------------------------------------------------------------------
300+
|
301+
| Here are each of the database connections setup for your application.
302+
| Of course, examples of configuring each database platform that is
303+
| supported by eloquent is shown below to make development simple.
304+
|
305+
|
306+
| All database work in eloquent is done through the PHP PDO facilities
307+
| so make sure you have the driver for your particular database of
308+
| choice installed on your machine before you begin development.
309+
|
310+
*/
311+
'connections' => [
312+
'sqlite' => [
313+
'driver' => 'sqlite',
314+
'url' => _env('DATABASE_URL'),
315+
'database' => _env('DB_DATABASE', AppPaths('databaseStorage') . '/database.sqlite'),
316+
'prefix' => '',
317+
'foreign_key_constraints' => _env('DB_FOREIGN_KEYS', true),
318+
],
319+
320+
'pgsql' => [
321+
'driver' => 'pgsql',
322+
'url' => _env('DATABASE_URL'),
323+
'host' => _env('DB_HOST', '127.0.0.1'),
324+
'port' => _env('DB_PORT', '5432'),
325+
'database' => _env('DB_DATABASE', 'forge'),
326+
'username' => _env('DB_USERNAME', 'forge'),
327+
'password' => _env('DB_PASSWORD', ''),
328+
'charset' => _env('DB_CHARSET', 'utf8'),
329+
'prefix' => '',
330+
'prefix_indexes' => true,
331+
'schema' => 'public',
332+
'sslmode' => 'prefer',
333+
],
334+
335+
'analytics' => [
336+
'driver' => 'pgsql',
337+
'url' => _env('ANALYTICS_DATABASE_URL'),
338+
'host' => _env('ANALYTICS_DB_HOST', '127.0.0.1'),
339+
'port' => _env('ANALYTICS_DB_PORT', '5432'),
340+
'database' => _env('ANALYTICS_DB_DATABASE', 'forge'),
341+
'username' => _env('ANALYTICS_DB_USERNAME', 'forge'),
342+
'password' => _env('ANALYTICS_DB_PASSWORD', ''),
343+
'charset' => _env('ANALYTICS_DB_CHARSET', 'utf8'),
344+
'prefix' => '',
345+
'prefix_indexes' => true,
346+
'schema' => 'public',
347+
'sslmode' => 'prefer',
348+
],
349+
350+
'imports' => [
351+
'driver' => 'pgsql',
352+
'url' => _env('IMPORTS_DATABASE_URL'),
353+
'host' => _env('IMPORTS_DB_HOST', '127.0.0.1'),
354+
'port' => _env('IMPORTS_DB_PORT', '5432'),
355+
'database' => _env('IMPORTS_DB_DATABASE', 'forge'),
356+
'username' => _env('IMPORTS_DB_USERNAME', 'forge'),
357+
'password' => _env('IMPORTS_DB_PASSWORD', ''),
358+
'charset' => _env('IMPORTS_DB_CHARSET', 'utf8'),
359+
'prefix' => '',
360+
'prefix_indexes' => true,
361+
'schema' => 'public',
362+
'sslmode' => 'prefer',
363+
],
364+
],
365+
];
366+
```
367+
368+
This example defines three PostgreSQL connections: `pgsql`, `analytics`, and `imports`, and an SQLite connection. You can then specify which connection a model should use by setting the `$connection` property in the model class as shown above.
369+
370+
:::
371+
275372
## Default Attribute Values
276373

277374
If you would like to define the default values for some of your model's attributes, you may define an $attributes property on your model:

0 commit comments

Comments
 (0)