Laravel 7:在每个月的第一天自动将值添加到列

发布于 2025-02-04 15:40:52 字数 1265 浏览 4 评论 0原文

我想每个月在表中的所有行中为特定列增加值。

用户表:

ID离开_Allocfeluf_earn
1200
2201.5
320-3
4204

控制器:

public function earned_leave(){
  $user = User::where('leave_alloc', '!=', null)->get();
  foreach($user as $row){
    $leave_alloc = $row->leave_alloc;
    $earned_leave = $leave_alloc / 12;
    $row->leave_earn += $earned_leave;
    $row->save();
  }
}

$ enned_leave将是$ wept_alloc除以的结果12。因此,在每个月的第一天,我想将赢得的休假金额添加到所有用户的well_earn列。此列可以具有负值和小数值。

我该如何进行自动化?

I want to add value to specific column for all rows in a table every month.

Users Table:

idleave_allocleave_earn
1200
2201.5
320-3
4204

Controller:

public function earned_leave(){
  $user = User::where('leave_alloc', '!=', null)->get();
  foreach($user as $row){
    $leave_alloc = $row->leave_alloc;
    $earned_leave = $leave_alloc / 12;
    $row->leave_earn += $earned_leave;
    $row->save();
  }
}

Here $earned_leave will be the result of $leave_alloc divided by 12. So, on the first day of every month, I want to add the earned leave amount to the leave_earn column of all users. This column can hold a negative value and decimal value.

How can I make this automation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-02-11 15:40:53

laravel支持任务计划。对于您的任务,您可以使用- > monthlyon(4,'15:00');

在最简单的实现中,看起来

$schedule->call(function () {
  $user = User::where('leave_alloc', '!=', null)->get();
  foreach($user as $row){
    $earned_leave = $row->leave_alloc / 12;
    $row->leave_earn += $earned_leave;
    $row->save();
  }
})->monthlyOn(1, '12:00');

别忘了添加

Laravel support task scheduling. For your task you may use ->monthlyOn(4, '15:00');

In most simple realization it looks like

$schedule->call(function () {
  $user = User::where('leave_alloc', '!=', null)->get();
  foreach($user as $row){
    $earned_leave = $row->leave_alloc / 12;
    $row->leave_earn += $earned_leave;
    $row->save();
  }
})->monthlyOn(1, '12:00');

Don't forget to add Cron entry

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文