Laravel自动更新数据库 [英] Laravel automatic update on database

查看:379
本文介绍了Laravel自动更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Laravel创建了会员系统,我想知道如何在日期过期后自动自动更新有效"列.

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

我的桌子:

membership(id, dateBegin, dateEnd, active)

(日期结束>现在)有效= 0时

when (dateEnd > NOW) active = 0

谢谢

推荐答案

由于您使用Laravel,因此可以利用其任务计划来完成此任务.请参阅文档的定义时间表部分.

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.

这篇关于Laravel自动更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆