PHP code example of ditscheri / laravel-check-constraints
1. Go to this page and download the library: Download ditscheri/laravel-check-constraints library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ditscheri / laravel-check-constraints example snippets
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');
});
Event::first()->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.
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 <= price');
});
Schema::table('users', function (Blueprint $table) {
$table->check('age > 18');
});
Schema::table('users', function (Blueprint $table) {
$table->check('age > 18', 'ny');
});
Schema::table('users', function (Blueprint $table) {
$table->dropCheck('