Download the PHP package ditscheri/laravel-check-constraints without Composer
On this page you can find all versions of the php package ditscheri/laravel-check-constraints. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ditscheri/laravel-check-constraints
More information about ditscheri/laravel-check-constraints
Files in ditscheri/laravel-check-constraints
Package laravel-check-constraints
Short Description Add check constraints to your Laravel schema.
License MIT
Homepage https://github.com/ditscheri/laravel-check-constraints
Informations about the package laravel-check-constraints
Add check constraints to your Laravel schema
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, PostrgeSQL, SQLite and SQL Server.
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
You can install the package via composer:
Usage
That last statement will produce the following SQL:
Now your database will only allow inserts and updates for rows with a valid date range.
If you try to insert or update a row that violates the check, an \Illuminate\Database\QueryException
will be thrown:
Another simple yet typical use case is with prices and discounts:
Of course you will still want to validate your data within the application code and detect such things before even reaching out to the database. But sometimes it is useful to have an additional layer of integrity checks right in your database itself.
Especially when you read data back from your database, your code may now safely assume that all the defined checks are guaranteed.
You can also add checks to existing tables:
Use the second parameter for an optional custom constraint name:
You can drop check constraints by their name:
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
You can publish the config file with:
This is the contents of the published config file:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Daniel Bakan
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-check-constraints with dependencies
spatie/laravel-package-tools Version ^1.9.2
illuminate/contracts Version ^8.0|^9.0|^10.0