PHPackages                             calebdw/laravel-sql-entities - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. calebdw/laravel-sql-entities

ActiveLibrary[Database &amp; ORM](/categories/database)

calebdw/laravel-sql-entities
============================

Manage SQL entities in Laravel with ease.

v0.7.0(2mo ago)311.6k↓71.4%MITPHPPHP ^8.4CI passing

Since Apr 13Pushed 6d ago1 watchersCompare

[ Source](https://github.com/calebdw/laravel-sql-entities)[ Packagist](https://packagist.org/packages/calebdw/laravel-sql-entities)[ Docs](https://github.com/calebdw/laravel-sql-entities)[ GitHub Sponsors](https://github.com/calebdw)[ RSS](/packages/calebdw-laravel-sql-entities/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (9)Dependencies (16)Versions (12)Used By (0)

 [![SQL Entities](/art/sql-entities.jpg)](/art/sql-entities.jpg)

Manage SQL entities in [Laravel](https://laravel.com) with ease!

 [![Laravel Compatibility](https://camo.githubusercontent.com/d1d16bca9cc70775537a9931d8bf54b51f41cdad4b9e2c96a584f50a90a6b426/68747470733a2f2f62616467652e6c61726176656c2e636c6f75642f62616467652f63616c656264772f6c61726176656c2d73716c2d656e746974696573)](https://packagist.org/packages/calebdw/laravel-sql-entities) [![Test Results](https://github.com/calebdw/laravel-sql-entities/actions/workflows/tests.yml/badge.svg)](https://github.com/calebdw/laravel-sql-entities/actions/workflows/tests.yml) [![Code Coverage](https://camo.githubusercontent.com/3e7a5986aecdc5e5d75682e52489431d235fdb92447e35314ec1f6b285c3acbc/68747470733a2f2f636f6465636f762e696f2f6769746875622f63616c656264772f6c61726176656c2d73716c2d656e7469746965732f67726170682f62616467652e7376673f746f6b656e3d52504c514b57444d3547)](https://codecov.io/github/calebdw/laravel-sql-entities) [![License](https://camo.githubusercontent.com/a6a6d4b68c81327315c2fc16e5dd6df73870386152532b1d845b9b8795f4e0a9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f63616c656264772f6c61726176656c2d73716c2d656e746974696573)](https://github.com/calebdw/laravel-sql-entities) [![Packagist Version](https://camo.githubusercontent.com/97a83e8ce26ee7daa852812851d9b68e9631660748064bed43523c05d39d2c92/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63616c656264772f6c61726176656c2d73716c2d656e7469746965732e737667)](https://packagist.org/packages/calebdw/laravel-sql-entities) [![Total Downloads](https://camo.githubusercontent.com/6ac538ae7c4ec01c5cd518e522f25fae9aadee049f9ba8c654fab7f97906d672/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63616c656264772f6c61726176656c2d73716c2d656e7469746965732e737667)](https://packagist.org/packages/calebdw/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](https://www.twitch.tv/theprimeagen/clip/DrabAltruisticEggnogVoHiYo-f6CVkrqraPsWrEht)

📦 Installation
--------------

[](#-installation)

First pull in the package using Composer:

```
composer require calebdw/laravel-sql-entities
```

Optionally, publish the configuration file:

```
php artisan vendor:publish --tag=sql-entities-config
```

The package looks for SQL entities under `database/entities/` so you might need to add a namespace to your `composer.json` file, for example:

```
{
  "autoload": {
    "psr-4": {
      "App\\": "app/",
+     "Database\\Entities\\": "database/entities/",
      "Database\\Factories\\": "database/factories/",
      "Database\\Seeders\\": "database/seeders/"
    }
  }
}
```

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.

Configuration
-------------

[](#configuration)

The package ships with a configuration file that controls automatic syncing behavior:

OptionDefaultDescription`sync``false`Automatically sync (refresh) entities whenever migrations run.`drop_on_migrate``true`Drop all entities before migrations start and recreate them after. When `false`, entities are only refreshed after migrations finish.When `drop_on_migrate` is enabled, all entities are dropped before migrations begin to prevent failures caused by dependent schema changes (e.g., dropping a column that a view references). However, this means entities will be unavailable while migrations are running, which can be problematic if the application is still serving requests.

When disabled, entities are refreshed (using `CREATE OR REPLACE`) after migrations finish. If a refresh fails due to a schema change, the entity is automatically dropped and recreated. For migrations that require specific entities to be absent, you can use the [`withoutEntities()`](#%EF%B8%8F-withoutentities) method for more granular control.

🛠️ Usage
--------

[](#️-usage)

### 🧱 SQL Entities

[](#-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:

```
