【问题标题】:Laravel Voyager, update on 1 table, update on anotherLaravel Voyager,更新一张桌子,更新另一张桌子
【发布时间】:2019-02-25 08:40:04
【问题描述】:
  • 语言:PHP
  • 框架:Laravel
  • 依赖/库:Laravel Voyager

我有 3 个数据库表:UsersSchedule of ReviewAppointments

用户可以预约很多日程,日程也可以被很多用户预约,所以基本上Appointments表就是我连接Users的关联实体>审核时间表表格

在我安装的 Laravel Voyager 中,Appointments 表有一列 status 被标记为 Paid / Not yet Paid

现在,每当管理员将 status 更改为 PAID,我想要 Schedule of Appointments 中的 slots 列 strong> 表减或减 1 以从字面上标记用户已成功支付他/她的时间审查时间表

我可以知道如何或在哪里添加此类代码,以便当 Laravel Voyager 更新 Appointments 表时,我也可以更新 Schedule of Reviews 表吗?

非常感谢!

【问题讨论】:

    标签: php database laravel web-applications voyager


    【解决方案1】:

    实现此特定场景的典型方法是重写模型,您可以在模型中实现逻辑,我假设为 App\ScheduleAppointments:

        namespace App;
    
        use Illuminate\Database\Eloquent\Model; 
    
        class ScheduleAppointments extends Model 
        {
            //Your model logic
            //
            //
            //
            //
    
        /**
         * OVERRIDE Boot Method
         */
            protected static function boot()
            {
                parent::boot();
                static::updating(function ($appointment) {
                if($appointment->status == 'PAID'){
                     $appointment->slot -=1;
                }    
                });
            }
    
        }
    

    【讨论】:

    • 但是,对于这个,如果我要更新约会的详细信息,即使不是“状态”列,它也会使插槽减少,有没有办法让我指定管理员已经更新了状态,那我只能在这时候减少槽位了吗?
    • 是的,模型中会有约会对象,基本上可以检查 if($appointment->status == 'PAID'){ $appointment->slot -=1;}这样,您将仅在状态更改的情况下进行更新
    • 谢谢萨阿德先生! Laravel 事件完成了这项工作!非常感谢!
    猜你喜欢
    • 2011-10-09
    • 2020-07-29
    • 2012-05-23
    • 2015-06-20
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    相关资源
    最近更新 更多