Download the PHP package calebdw/laravel-sql-entities without Composer
On this page you can find all versions of the php package calebdw/laravel-sql-entities. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download calebdw/laravel-sql-entities
More information about calebdw/laravel-sql-entities
Files in calebdw/laravel-sql-entities
Package laravel-sql-entities
Short Description Manage SQL entities in Laravel with ease.
License MIT
Homepage https://github.com/calebdw/laravel-sql-entities
Informations about the package laravel-sql-entities
Laravel's schema builder and migration system are great for managing tables and indexes---but offer no built-in support for other SQL entities, such as (materialized) views, procedures, functions, and triggers. These often get handled via raw SQL in migrations, making them hard to manage, prone to unknown conflicts, and difficult to track over time.
laravel-sql-entities
solves this by offering:
- π¦ Class-based definitions: bringing views, functions, triggers, and more into your application code.
- π§ First-class source control: you can easily track changes, review diffs, and resolve conflicts.
- π§± Decoupled grammars: letting you support multiple drivers without needing dialect-specific SQL.
- π Lifecycle hooks: run logic at various points, enabling logging, auditing, and more.
- π Batch operations: easily create or drop all entities in a single command or lifecycle event.
- π§ͺ Testability: definitions are just code so theyβre easy to test, validate, and keep consistent.
Whether you're managing reporting views, business logic functions, or automation triggers, this package helps you treat SQL entities like real, versioned parts of your codebase---no more scattered SQL in migrations!
[!NOTE] Migration rollbacks are not supported since the definitions always reflect the latest state.
"We're never going backwards. You only go forward." -Taylor Otwell
π¦ Installation
First pull in the package using Composer:
The package looks for SQL entities under database/entities/
so you might need to add
a namespace to your composer.json
file, for example:
[!TIP] This package looks for any files matching
database/entities
in the application's base path. This means it should automatically work for a modular setup where the entities might be spread across multiple directories.
π οΈ Usage
π§± SQL Entities
To get started, create a new class in a database/entities/
directory
(structure is up to you) and extend the appropriate entity class (e.g. View
, etc.).
For example, to create a view for recent orders, you might create the following class:
You can also override the name and connection:
π Lifecycle Hooks
You can also use the provided lifecycle hooks to run logic before or after an entity is created or dropped.
Returning false
from the creating
or dropping
methods will prevent the entity from being created or dropped, respectively.
βοΈ Handling Dependencies
Entities may depend on one another (e.g., a view that selects from another view).
To support this, each entity can declare its dependencies using the dependencies()
method:
The manager will ensure that dependencies are created in the correct order, using a topological sort behind the scenes.
In the example above, OrdersView
will be created before RecentOrdersView
automatically.
π View
The View
class is used to create views in the database.
In addition to the options above, you can use the following options to further customize the view:
π Function
The Function_
class is used to create functions in the database.
[!TIP] The class is named
Function_
asfunction
is a reserved keyword in PHP.
In addition to the options above, you can use the following options to further customize the function:
Loadable functions are also supported:
β‘ Trigger
The Trigger
class is used to create triggers in the database.
In addition to the options above, you can use the following options to further customize the trigger:
π§ Manager
The SqlEntityManager
singleton is responsible for creating and dropping SQL entities at runtime.
You can interact with it directly, or use the SqlEntity
facade for convenience.
β»οΈ withoutEntities()
Sometimes you need to run a block of logic (like renaming a table column) without certain SQL entities present.
The withoutEntities()
method temporarily drops the selected entities, executes your callback, and then recreates them afterward.
If the database connection supports schema transactions, the entire operation is wrapped in one.
You can also restrict the scope to certain entity types or connections:
After the callback, all affected entities are automatically recreated in dependency order.
π» Console Commands
The package provides console commands to create and drop your SQL entities.
π Automatic syncing when migrating (Optional)
You may want to automatically drop all SQL entities before migrating, and then recreate them after the migrations are complete. This is helpful when the entities depend on schema changes. To do this, register the built-in subscriber in a service provider:
The listener will also create all entities if there's no pending migrations, ensuring any new entities are created automatically.
π€ Contributing
Thank you for considering contributing! You can read the contribution guide here.
βοΈ License
This is open-sourced software licensed under the MIT license.
π Alternatives
All versions of laravel-sql-entities with dependencies
illuminate/console Version ^11.0 || ^12.0
illuminate/contracts Version ^11.0 || ^12.0
illuminate/database Version ^11.0 || ^12.0
illuminate/support Version ^11.0 || ^12.0