jeudi 12 octobre 2017

Virtual column based on current date

Is it possible to use the current date in virtual columns?

This is what I tried so far:

I've created the following migration.

Schema::create('contracts', function (Blueprint $table) {
    $table->increments('id');
    $table->date('expires_at');
    $table->boolean('is_expired')->virtualAs('expires_at < CURRENT_DATE()');
});

Which outputs in the following query:

create table `contracts` (
    `id` int unsigned not null auto_increment primary key,
    `expires_at` date not null,
    `is_expired` tinyint(1) as (expires_at < CURRENT_DATE())
) default character set utf8mb4 collate utf8mb4_unicode_ci

The error message I am getting:

Expression of generated column 'is_expired' contains a disallowed function.

So, I know you can't use virtual columns this way. Is there an other way to make this column generated? Maybe something with triggers?

Because I am using Laravel for this project, I would be happy with a PHP solution.

Maybe there is an options to put raw sql statements in the $appends array? That would also help. As long as I can execute WHERE is_expired = false.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire