Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

laravel - 如何检查月份是否更改?(How to check if month change?)

I want to update all my depriciation cost each month for all items until 0

(我想每月更新所有项目的所有折旧费用,直到0)

Example.

(例。)

Depriciation has value of 111 . each month it should be depreciated until depriciation cost 
reach 0 or less than 

在此处输入图片说明

controller

(控制者)

all_item=assets::all();

foreach($all_item as $item)
{
     //should i make static month to compare?

     $month=Carbon::now()->format('m');

     $update_item = assets::findOrFail($item->id);
     $update_item->depriciation_cost = $item->depriciation - $item->depriciation_cost;
     $update_item->update();
}
  ask by jer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a new column in the table to keep track of the last depreciation value updated like depreciation_updated_at (type: date) OR use created_at/updated_at column to get the last update month (if no other reason to update record).

(您可以在表中创建一个新列,以跟踪上次更新的折旧值,如depreciation_updated_at (类型:日期),或使用created_at/updated_at列获取上一个更新月份(如果无其他原因更新记录)。)

Then simply use updated version of your code:

(然后,只需使用代码的更新版本:)

all_item=assets::all();

foreach($all_item as $item)
{
   $month=Carbon::now()->format('m');
   $depreciation_updated_at = createFromFormat('Y-m-d', $item->depreciation_updated_at); //or can use created_at/updated_at as said below
   if ($depreciation_updated_at ->format('m') != $month) {
      $update_item = assets::findOrFail($item->id);
      $update_item->depriciation_cost = $item->depriciation - $item->depriciation_cost;
      $update_item->update();
   }
}

Hope it will do your task.

(希望它能完成您的任务。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...