Laravel automatic update on database

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 AppConsole;

...

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x