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
Leaf MVC inherited all the teachings of Laravel and Ruby on Rails, including the use of migrations, seeders, and factories which made creating, testing and seeding databases a breeze. While this is great and has been tried and tested over the years, having multiple files for a single database table can be a bit of a hassle. This is why we introduced Schema Files in Leaf MVC v4.
3
+
Leaf MVC took inspiration from Laravel and Ruby on Rails, adopting migrations, seeders, and factories to make database management easy. While this approach is powerful, managing multiple files for a single table can become a hassle.
4
+
5
+
That’s why in Leaf MVC v4, we’re introducing Schema Files—a simpler, more intuitive way to define, test, and seed your database in one place.
4
6
5
7
## What are Schema Files?
6
8
7
-
Schema files build on the JSON schema idea we introduced in earlier Leaf MVC versions, but they take things further. Instead of juggling separate files for migrations, seeders, and factories, you can handle everything in one place. They’re written in YAML, so they’re easy to read and work with—no extra hassle, no repeating yourself.
9
+
Schema Files build on the JSON schema idea from earlier Leaf MVC versions but take things even further. Instead of managing separate migrations, seeders, and factories, everything is now in one place. Written in YAML, they’re clean, easy to read, and eliminate unnecessary repetition—so you can focus on building, not juggling files.
Migration histories are used to keep track of the migrations that have been run on your database. This is useful for keeping track of the state of your database so you can easily roll back to a previous state if needed. Unlike in other frameworks, Leaf MVC does require you to manually create migrations to keep track of your migration history. This is done automatically for you.
179
-
180
-
Every time you edit a schema file and run the `db:migrate` command, Leaf will automatically keep track of the migrations that have been run on your database, which means less time scrambling through migration files and more time building your app.
180
+
Migration histories keep track of changes to your database, making it easy to roll back if needed. Unlike other frameworks, **Leaf MVC handles this automatically**—no need to manually create migrations just to track history.
181
181
182
-
In the end, this means you can continue to use `php leaf db:rollback` to roll back your database to a previous state.
182
+
Every time you update a Schema File and run `db:migrate`, Leaf logs the changes for you. No more digging through migration files—just focus on building. And when you need to roll back? Simply run `php leaf db:rollback`.
183
183
184
184
## Seeding your database
185
185
186
-
Database seeds are a way to populate a database with initial data. This initial data can be used to set up default values or pre-populate a database with test data. Database seeds typically contain small amounts of data, such as default settings, test data, or sample records.
186
+
Database seeds let you pre-populate your database with initial data—whether it's default settings, test data, or sample records. Instead of manually adding entries, you can use seeders to automate this process.
187
187
188
-
Seeders are used to populate your database with dummy data. In Leaf MVC, you can create seeders in your database files. The `seeds` keyin your database file is used to create seeders. Here's an example of a seeder:
188
+
In Leaf MVC, you define seeders directly in your Schema Files under the `seeds` key. This keeps everything in one place, making it easier to manage your database setup. Here's an example of a seeder:
Copy file name to clipboardExpand all lines: src/docs/database/models.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Models
2
2
3
-
A model is a class that represents your app's data. It acts like the middle-man between your database and your app, allowing you work with the data in your database without writing complicated database code. Models have some advantages over using raw SQL queries and even using a query builder like Leaf DB, some of these advantages include:
3
+
A model is a class that represents your app’s data, acting as a bridge between your database and application. Instead of [building complex SQL queries](/docs/database/builder), models let you work with your data in a clean, reusable, and organized way.
4
4
5
-
- Organization: Models keep your code organized by separating the database logic from other parts of your application (like views or controllers). This makes your code cleaner and easier to maintain.
5
+
## Why use models?
6
6
7
-
- Reusability: Once a model is set up, you can reuse it throughout your app to handle database interactions without duplicating code.
7
+
- Organization – Keeps database logic separate from your views and controllers, making your app cleaner and easier to maintain.
8
+
- Reusability – Define once, use anywhere—no need to repeat database code.
9
+
- Consistency – Enforces a structured way of interacting with data, reducing errors.
8
10
9
-
- Consistency: Models enforce a consistent way of working with your data, reducing the chances of errors when interacting with the database.
10
-
11
-
On top of all that, a single model corresponds to a single database table. This makes your data more organized and easier to work with.
11
+
Each model maps to a database table, keeping your data structured and easy to manage.
12
12
13
13
## Creating a model
14
14
@@ -139,7 +139,7 @@ $flight->delete(); // Delete the flight
139
139
140
140
A soft delete marks a record as deleted without actually removing it from the database. Instead, a `deleted_at` timestamp is set, and the record is hidden from query results. The data is still in the database, allowing you to restore it later if needed. To get started with soft deletes, add a `deleted_at` column to your table. You can do this by adding the following line to your [migration file](/docs/database/migrations):
Copy file name to clipboardExpand all lines: src/docs/database/redis.md
+1-28Lines changed: 1 addition & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,34 +70,7 @@ composer require leafs/redis
70
70
71
71
:::
72
72
73
-
Leaf Redis comes with a few commands that you can attach to the MVC console. You can do this by heading over to the `leaf` file in your project root and adding the following line to the return array.
| Leaf MVC allows you to customize Leaf and it's modules using
82
-
| configuration files defined in the config folder. This line
83
-
| loads the configuration files and makes them available to
84
-
| your application.
85
-
|
86
-
*/
87
-
Leaf\Core::loadConsole([
88
-
Leaf\Redis::commands() // [!code ++]
89
-
]);
90
-
```
91
-
92
-
Once you've done that, you should have access to a bunch of new commands from Leaf Redis. The available commands are:
93
-
94
-
```bash:no-line-numbers
95
-
redis
96
-
redis:install Create leaf redis config and .env variables
97
-
redis:server Start redis server
98
-
```
99
-
100
-
You won't need these commands in development because Leaf will automatically set up all the necessary files for you and will also start a Redis server whenever you start your application.
73
+
From there, restart your server and Leaf will automatically detect the Redis package and start a Redis process alongside your application.
Copy file name to clipboardExpand all lines: src/docs/frontend/bareui.md
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,10 +41,6 @@ composer require leafs/bareui
41
41
42
42
Once installed, BareUI will be available in your Leaf app on the `template()` method. This makes it easy to use BareUI from anywhere in your app.
43
43
44
-
```php:no-line-numbers
45
-
app()->template()->render('welcome');
46
-
```
47
-
48
44
## Configuring BareUI
49
45
50
46
BareUI doesn't require any real configuration to work, but you need to tell it where to look for your templates. You can do this using the `config()` method. If you are using Leaf MVC, this has already been done for you in the `config/view.php` file, so you can skip this step.
@@ -80,10 +76,10 @@ BareUI templates are regular PHP files, so you can create your templates using P
80
76
81
77
## Rendering Templates
82
78
83
-
Once you have your template, you can render it using the `render` method. This method takes in the name of the template to render and an array of data to pass to the template.
79
+
Once you have your template, you can render it using the `render()` method on Leaf's response. This method takes in the name of the template to render and an array of data to pass to the template.
84
80
85
81
```php:no-line-numbers
86
-
echo app()->template()->render('welcome');
82
+
response()->render('welcome');
87
83
```
88
84
89
85
When rendering a template, you don't need to include the `.view.php` extension in the template name. BareUI automatically adds it for you when it looks for the template file. So, you only need to pass the name of the template without the extension, and BareUI will handle the rest!
@@ -118,15 +114,15 @@ This is an empty HTML page with a PHP tag that echoes a variable. On its own, th
118
114
To pass data to this `$name` variable, you can pass an array of data as the second argument to the `render()` method. This array should contain the same keys as the variables you want to use in the template.
119
115
120
116
```php
121
-
echo app()->template()->render('welcome', [
117
+
response()->render('welcome', [
122
118
'name' => 'Something',
123
119
]);
124
120
```
125
121
126
122
This will render the template and replace the `$name` variable with the value `'Something'`. You can pass as many variables as you want to the template, and they will all be available in the template file. These values can be anything from strings to arrays, objects, or even functions.
Copy file name to clipboardExpand all lines: src/docs/frontend/blade.md
+56-37Lines changed: 56 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,7 @@ Blade views are a pretty sweet mixture of HTML, PHP, and clean syntax. You can c
90
90
91
91
This should look pretty familiar if you know HTML (of course you do). The only difference is the <spanv-pre>`{{ $name }}`</span> part. This is Blade's way of creating a variable in your view. When you render this view, Blade will allow you pass in a variable called `$name` and it will be displayed in place of <spanv-pre>`{{ $name }}`</span>. Let's see how you can render this view.
@@ -224,7 +211,7 @@ This should look pretty familiar if you know HTML (of course you do). The only d
224
211
Remember we set up Blade earlier? Now we can use it to render our Blade views. Here's how you can render the `hello.blade.php` view we created earlier:
This will render the `hello.blade.php` view and pass in a variable called `name` with the value `Michael`. When you open the view in your browser, you should see a big "Hello, Michael" on your screen.
@@ -387,7 +374,7 @@ Likewise, the @selected directive may be used to indicate if a given select opti
387
374
388
375
Additionally, the @disabled directive may be used to indicate if a given element should be "disabled":
| Some view engines like blade allow you extend the engine to
522
+
| add extra functions or directives. This is just the place to
523
+
| do all of that. Extend is a function that accepts an instance
524
+
| of your view engine which you can 'extend'
525
+
|
526
+
*/
527
+
'extend' => function (\Leaf\Blade $engine) {
528
+
$engine->directive('datetime', function ($expression) {
529
+
return "<?php echo tick({$expression})->format('DD MM YYYY'); ?>";
530
+
});
531
+
},
532
+
```
533
+
534
+
:::
516
535
517
536
Which allows you to use the following in your blade template:
518
537
519
538
```blade:no-line-numbers
520
539
Current date: @datetime($date)
521
540
```
522
541
523
-
This will output the current date in the format `F d, Y g:i a`. You can define as many custom directives as you want to make your Blade views more powerful.
542
+
This will output the current date in the format `DD MM YYYY`. You can define as many custom directives as you want to make your Blade views more powerful.
0 commit comments