PHPackages                             ditscheri/laravel-check-constraints - 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. ditscheri/laravel-check-constraints

ActiveLibrary

ditscheri/laravel-check-constraints
===================================

Add check constraints to your Laravel schema.

v0.0.9(4mo ago)628.3k↓12.5%2[4 PRs](https://github.com/ditscheri/laravel-check-constraints/pulls)MITPHPPHP ^8.2|^8.3|^8.4|^8.5CI passing

Since Feb 18Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/ditscheri/laravel-check-constraints)[ Packagist](https://packagist.org/packages/ditscheri/laravel-check-constraints)[ Docs](https://github.com/ditscheri/laravel-check-constraints)[ GitHub Sponsors](https://github.com/ditscheri)[ RSS](/packages/ditscheri-laravel-check-constraints/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (11)Versions (15)Used By (0)

Add check constraints to your Laravel schema
============================================

[](#add-check-constraints-to-your-laravel-schema)

[![Latest Version on Packagist](https://camo.githubusercontent.com/87a72532a34af3820f9b6ee2d9a92074aa9bf393c11e4d119b466439e227574d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6469747363686572692f6c61726176656c2d636865636b2d636f6e73747261696e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ditscheri/laravel-check-constraints)[![GitHub Tests Action Status](https://camo.githubusercontent.com/495e71bdf40bfd6a38a0f2ba2f654f3e08e1ba055d8da8131ba6068a965048d8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6469747363686572692f6c61726176656c2d636865636b2d636f6e73747261696e74732f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/ditscheri/laravel-check-constraints/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/28abc98323d72d8361fef76887ac244e3d356a7010498cadd07edddafa3b3223/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6469747363686572692f6c61726176656c2d636865636b2d636f6e73747261696e74732f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/ditscheri/laravel-check-constraints/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/87e6d8c9e9d9cddfffb5085fbc3c21d09db31294f87497b04ffd587cc4620108/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6469747363686572692f6c61726176656c2d636865636b2d636f6e73747261696e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ditscheri/laravel-check-constraints)

This packages allows you to add native check constraints to your database tables.

You can read more about check constraints in the official documentations of [MySQL](https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html), [PostrgeSQL](https://www.postgresql.org/docs/14/ddl-constraints.html), [SQLite](https://www.sqlite.org/lang_createtable.html#check_constraints) and [SQL Server](https://docs.microsoft.com/en-us/sql/relational-databases/tables/unique-constraints-and-check-constraints?view=sql-server-ver15#Check).

Currently, this package does not add check constraints to the SQLite driver, but you should be fine if you only use SQLite for running tests (see below).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require ditscheri/laravel-check-constraints
```

Usage
-----

[](#usage)

```
Schema::create('events', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->datetime('starts_at');
    $table->datetime('ends_at');

    // This is the new part:
    $table->check('starts_at < ends_at');
});
```

That last statement will produce the following SQL:

```
alter table `events` add constraint `events_starts_at_ends_at_check` check (starts_at update([
    'starts_at' => '2022-02-19 20:00:00',
    'end_at'    => '2022-02-19 18:00:00', // this one would be over before it even started?!
]);

// Illuminate\Database\QueryException with message
// SQLSTATE[HY000]: General error: 3819
// Check constraint 'events_starts_at_ends_at_check' is violated.
```

Another simple yet typical use case is with prices and discounts:

```
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->unsignedInteger('price');
    $table->unsignedInteger('discounted_price');

    // Ensure that discounts are lower than the regular price:
    $table->check('discounted_price  18');
});
```

Use the second parameter for an optional custom constraint name:

```
Schema::table('users', function (Blueprint $table) {
    $table->check('age > 18', 'require_min_age');
    $table->check('is_admin=1 OR company_id IS NOT NULL', 'non_admins_require_company');
});
```

You can drop check constraints by their name:

```
Schema::table('users', function (Blueprint $table) {
    $table->dropCheck('require_min_age');
});
```

A note about SQLite
-------------------

[](#a-note-about-sqlite)

While SQLite does support check constraints within `create table` statements, there are a number of limitions:

- SQLite cannot add check constraints to existing tables.
- SQLite cannot drop check constraints.

Since this package only relies on macros, it currently does not support the SQLite driver at all.

Instead, you can use the config `check-constraints.sqlite.throw` to define wether to throw a `RuntimeException` or to fail silently when using SQLite.

If you only use SQLite in your tests, you might be fine with setting the option to `false`. This gives you all the benefits of check constraints for your production environment, while your tests can still run using SQLite, where the calls to `$table->check()` will just be skipped.

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

[](#configuration)

You can publish the config file with:

```
php artisan vendor:publish --tag="check-constraints-config"
```

This is the contents of the published config file:

```
return [
    'sqlite' => [
        'throw' => true,
    ],
];
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Daniel Bakan](https://github.com/dbakan)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance83

Actively maintained with recent releases

Popularity32

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 64.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~178 days

Recently: every ~356 days

Total

9

Last Release

123d ago

PHP version history (2 changes)v0.0.1PHP ^8.0

v0.0.8PHP ^8.2|^8.3|^8.4|^8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/5bdde3bbcf564c64107c3df5cca4d8433ecade49248f7d8d0b302d0437e5b63d?d=identicon)[ditscheri](/maintainers/ditscheri)

---

Top Contributors

[![dbakan](https://avatars.githubusercontent.com/u/4498077?v=4)](https://github.com/dbakan "dbakan (43 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (13 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (11 commits)")

---

Tags

laravelditscherilaravel-check-constraints

###  Code Quality

TestsPest

Static AnalysisPHPStan

### Embed Badge

![Health badge](/badges/ditscheri-laravel-check-constraints/health.svg)

```
[![Health](https://phpackages.com/badges/ditscheri-laravel-check-constraints/health.svg)](https://phpackages.com/packages/ditscheri-laravel-check-constraints)
```

###  Alternatives

[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

24149.7k](/packages/vormkracht10-laravel-mails)[spatie/laravel-prometheus

Export Laravel metrics to Prometheus

2651.3M6](/packages/spatie-laravel-prometheus)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[musahmusah/laravel-multipayment-gateways

A Laravel Package that makes implementation of multiple payment Gateways endpoints and webhooks seamless

852.2k1](/packages/musahmusah-laravel-multipayment-gateways)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
