2 |
3 |
4 |
5 |
Manage SQL entities in Laravel with ease!
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | Laravel's schema builder and migration system are great for managing tables and
16 | indexes---but offer no built-in support for other SQL entities, such as
17 | (materialized) views, procedures, functions, and triggers.
18 | These often get handled via raw SQL in migrations, making them hard to manage,
19 | prone to unknown conflicts, and difficult to track over time.
20 |
21 | `laravel-sql-entities` solves this by offering:
22 |
23 | - 📦 Class-based definitions: bringing views, functions, triggers, and more into your application code.
24 | - 🧠 First-class source control: you can easily track changes, review diffs, and resolve conflicts.
25 | - 🧱 Decoupled grammars: letting you support multiple drivers without needing dialect-specific SQL.
26 | - 🔁 Lifecycle hooks: run logic at various points, enabling logging, auditing, and more.
27 | - 🚀 Batch operations: easily create or drop all entities in a single command or lifecycle event.
28 | - 🧪 Testability: definitions are just code so they’re easy to test, validate, and keep consistent.
29 |
30 | Whether you're managing reporting views, business logic functions, or automation
31 | triggers, this package helps you treat SQL entities like real, versioned parts
32 | of your codebase---no more scattered SQL in migrations!
33 |
34 | > [!NOTE]
35 | > Migration rollbacks are not supported since the definitions always reflect the latest state.
36 | >
37 | > ["We're never going backwards. You only go forward." -Taylor Otwell](https://www.twitch.tv/theprimeagen/clip/DrabAltruisticEggnogVoHiYo-f6CVkrqraPsWrEht)
38 |
39 | ## 📦 Installation
40 |
41 | First pull in the package using Composer:
42 |
43 | ```bash
44 | composer require calebdw/laravel-sql-entities
45 | ```
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | The package looks for SQL entities under `database/entities/` so you might need to add
54 | a namespace to your `composer.json` file, for example:
55 |
56 | ```diff
57 | {
58 | "autoload": {
59 | "psr-4": {
60 | "App\\": "app/",
61 | + "Database\\Entities\\": "database/entities/",
62 | "Database\\Factories\\": "database/factories/",
63 | "Database\\Seeders\\": "database/seeders/"
64 | }
65 | }
66 | }
67 | ```
68 |
69 | > [!TIP]
70 | > This package looks for any files matching `database/entities` in the application's
71 | > base path. This means it should automatically work for a modular setup where
72 | > the entities might be spread across multiple directories.
73 |
74 |
75 |
76 | ## 🛠️ Usage
77 |
78 | ### 🧱 SQL Entities
79 |
80 | To get started, create a new class in a `database/entities/` directory
81 | (structure is up to you) and extend the appropriate entity class (e.g. `View`, etc.).
82 |
83 | For example, to create a view for recent orders, you might create the following class:
84 |
85 | ```php
86 | select(['id', 'customer_id', 'status', 'created_at'])
103 | ->where('created_at', '>=', now()->subDays(30))
104 | ->toBase();
105 |
106 | // could also use raw SQL
107 | return <<<'SQL'
108 | SELECT id, customer_id, status, created_at
109 | FROM orders
110 | WHERE created_at >= NOW() - INTERVAL '30 days'
111 | SQL;
112 | }
113 | }
114 | ```
115 |
116 | You can also override the name and connection:
117 |
118 | ```php
119 | connection->statement(<<