You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/docs/database/index.md
+46-26Lines changed: 46 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,18 +66,45 @@ db()->connect([
66
66
]);
67
67
```
68
68
69
-
The `connect()` method takes an array of connection details for your database as its argument. Depending on the database system you're using, you'll need to provide different connection details. For example, here's how you can connect to a SQLite database:
69
+
The `connect()` method takes an array of connection details for your database as its argument. Depending on the database system you're using, you'll need to provide different connection details.
70
+
Here are some examples of how you can connect to different databases:
70
71
71
-
```php
72
+
::: code-group
73
+
74
+
```php [MySQL]
75
+
db()->connect([
76
+
'host' => '127.0.0.1',
77
+
'username' => 'root',
78
+
'password' => '',
79
+
'dbname' => 'Leaf',
80
+
]);
81
+
```
82
+
83
+
```php [PostgreSQL]
84
+
db()->connect([
85
+
'dbtype' => 'pgsql',
86
+
'host' => '127.0.0.1',
87
+
'username' => 'root',
88
+
'password' => '',
89
+
'dbname' => 'Leaf',
90
+
'port' => '5432',
91
+
]);
92
+
```
93
+
94
+
```php [SQLite]
72
95
db()->connect([
73
96
'dbtype' => 'sqlite',
74
97
'dbname' => 'db.sqlite',
75
98
]);
76
99
```
77
100
78
-
If you have a environment file, you can use it to store your database connection details. Here's an example of how you can connect to a MySQL database using an environment file:
101
+
:::
102
+
103
+
If you are using Leaf MVC, we have already set up everything for you. All you need to do is to head over to your `.env` file and set up your database connection details.
104
+
105
+
::: code-group
79
106
80
-
```txt
107
+
```txt [MySQL]
81
108
DB_CONNECTION=mysql
82
109
DB_HOST=127.0.0.1
83
110
DB_PORT=3306
@@ -86,12 +113,24 @@ DB_USERNAME=root
86
113
DB_PASSWORD=
87
114
```
88
115
89
-
Using the environment file, you can connect to the database like this:
116
+
```txt [PostgreSQL]
117
+
DB_CONNECTION=pgsql
118
+
DB_HOST=127.0.0.1
119
+
DB_PORT=5432
120
+
DB_DATABASE=LeafMVC
121
+
DB_USERNAME=root
122
+
DB_PASSWORD=
123
+
```
90
124
91
-
```php
92
-
db()->autoConnect();
125
+
```txt [SQLite]
126
+
DB_CONNECTION=sqlite
127
+
DB_DATABASE=/absolute/path/to/database.sqlite
93
128
```
94
129
130
+
:::
131
+
132
+
Remember to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
133
+
95
134
## Writing simple queries
96
135
97
136
Once you've connected to a database, you can start writing queries to interact with it. Queries are the commands you run on your database to get, insert, update or delete data. Leaf DB provides a simple way to run queries using the query builder, but also allows you to run raw SQL queries.
@@ -147,22 +186,3 @@ There may be times when you want to get a single value from a query that returns
147
186
```php
148
187
$user = db()->query('SELECT * FROM users')->first();
149
188
```
150
-
151
-
## Leaf DB + MVC
152
-
153
-
Leaf MVC comes with a more structured way to work with databases. You can use models to interact with your database. This makes it easier to manage your database operations and keep your code clean. However, you can still use the query builder to run raw queries if you need to.
154
-
155
-
To get started, you need to set up your database connection in your environment file.
156
-
157
-
```txt
158
-
DB_CONNECTION=mysql
159
-
DB_HOST=127.0.0.1
160
-
DB_PORT=3306
161
-
DB_DATABASE=LeafMVC
162
-
DB_USERNAME=root
163
-
DB_PASSWORD=
164
-
```
165
-
166
-
From there, you just need to head over to `public/index.php` and uncomment the line that says `\Leaf\Database::initDb();`. This will automatically connect to your database using the details in your environment file.
167
-
168
-
If you have multiple databases, Leaf DB will connect to your database which has been labelled as your default database. Leaf DB does not yet support multiple database connections, but of course, you can manually create multiple instances of Leaf DB.
You can then redirect to this route using the route name by passing an array with the route name to the `redirect()` method.
114
+
115
+
```php:no-line-numbers
116
+
response()->redirect(['home']);
117
+
```
118
+
119
+
If you want to get details about a route, you can use the `getRoute()` method.
120
+
121
+
```php:no-line-numbers
122
+
$route = app()->route($routeName);
123
+
```
124
+
125
+
This will return an array containing the following information:
126
+
127
+
-`pattern`: The route pattern
128
+
-`path`: The route path
129
+
-`name`: The route name
130
+
-`method`: The route method
131
+
-`handler`: The route handler
132
+
- Any other route options
133
+
113
134
## Getting the current route
114
135
115
136
There are times when you need to get the current route which the user is visiting from inside your route handler. You can do this by calling the `getRoute()` method on the router instance.
@@ -132,24 +153,16 @@ This method returns an array containing the following information:
132
153
133
154
## Navigating to another route
134
155
135
-
There are times when you need to redirect users to another route. For example, after a user logs in, you might want to redirect them to their dashboard. You can do this by calling the `push()` method on the router instance or the `redirect()` method on the response instance.
156
+
There are times when you need to redirect users to another route. For example, after a user logs in, you might want to redirect them to their dashboard. You can do this by calling the `redirect()` method on the response instance.
136
157
137
-
::: code-group
138
-
139
-
```php:no-line-numbers [router]
140
-
app()->push('/login');
141
-
```
142
-
143
-
```php:no-line-numbers [response]
158
+
```php:no-line-numbers
144
159
response()->redirect('/login');
145
160
```
146
161
147
-
:::
148
-
149
-
If your route has a name, you can navigate to it by passing the route name in an array to the `push()` method.
162
+
If your route has a name, you can navigate to it by passing the route name in an array to the `redirect()` method.
0 commit comments