Laravel automatic update on database

- 1 answer

Ad

I created a membership system using Laravel and I would like to know how automatically update the "active" column when the date is expired.

My table :

membership(id, dateBegin, dateEnd, active)

when (dateEnd > NOW) active = 0

Thank's

Ad

Answer

Ad

Since your using Laravel you could make use of its Task Scheduling to accomplish this. See the Defining Schedules section of the documentation.

Based on the example given you could do something like

<?php

namespace App\Console;

...

class Kernel extends ConsoleKernel
{
    ... 

    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            DB::table('membership')->whereRaw('dateEnd > now()')->update(['active' => 0]);
        })->daily();
    }
}

I haven't tested the above but it should work as you require.

Ad
source: stackoverflow.com
Ad