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.
(希望它能完成您的任务。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…