php - 如何在 Laravel 6 中为自定义唯一增量添加年份
问题描述
我在我的控制器中使用的程序中有一个自定义的唯一增量,因此每个项目都不会具有相同的标识符
控制器
public function store(Request $request)
{
$request->validate([
'device_type_name' => 'required',
'device_brand_name' => 'required',
'device_status' => 'required',
]);
Transactions_in::getidtransactionsin();
$employees = new Transactions_in();
$employees->idDevice = "0";
$employees->DeviceType_id = $request->input('device_type_name');
$employees->DeviceBrand_id = $request->input('device_brand_name');
$employees->DeviceStatus_id = $request->input('device_status');
$employees->save();
$employees->update(['idDevice' => sprintf('NPC.ABC.%03d', $employees->id)]);
return redirect('/transactionsin')->with('success', 'New Item Added');
}
模型
class Transactions_in extends Model
{
protected $guarded = [];
public static function getidtransactionsin()
{
DB::table('transactions_ins')->orderBy('id', 'desc')->take(1)->get();
}
public function get_devicetypes()
{
return $this->belongsTo(DeviceType::class, 'DeviceType_id');
}
public function get_devicebrands()
{
return $this->belongsTo(DeviceBrand::class, 'DeviceBrand_id');
}
public function get_devicestatuses()
{
return $this->belongsTo(DeviceStatus::class, 'DeviceStatus_id');
}
}
现在idDevice
将像这样保存
| id | idDevice |
+----+---------------+
| 1 | NPC.ABC.001 |
| 2 | NPC.ABC.002 |
| 3 | NPC.ABC.003 |
有没有办法在那里添加当年所以输出会是这样的
| id | idDevice |
+----+---------------------+
| 1 | NPC.ABC.001.2019 |
| 2 | NPC.ABC.002.2019 |
| 3 | NPC.ABC.003.2019 |
还有一件事我可以做到,所以明年idDevice
将再次从 1 开始。
| id | idDevice |
+----+---------------------+
| 1 | NPC.ABC.001.2019 |
| 2 | NPC.ABC.002.2019 |
| 3 | NPC.ABC.003.2019 |
| 4 | NPC.ABC.001.2020 |
| 5 | NPC.ABC.002.2020 |
| 6 | NPC.ABC.003.2020 |
| 7 | NPC.ABC.001.2021 |
| 8 | NPC.ABC.002.2021 |
| 9 | NPC.ABC.003.2021 |
解决方案
拳头从数据库中获取最新记录并根据年份增加id
$latest_idDevice = Transactions_in::latest('id')->first();
if(isset($latest_idDevice->idDevice) && $latest_idDevice->idDevice!=null){
$arr = explode('.',$latest_idDevice->idDevice); // convert string to array
if($arr[3] == date('Y')){ //check that last record has current year
// increment id
$idDevice = 'NPC.ABC.'.($arr[2]+1).'.'.date('Y');
}
else { // last record belongs to previous year so add this year record with id 1
$idDevice = 'NPC.ABC.1.'.date('Y');
}
}
else { // there is no record in database so first record of year
$idDevice = 'NPC.ABC.1.'.date('Y');
}
这是您添加记录的代码
$employees = new Transactions_in();
$employees->idDevice = $idDevice;
$employees->DeviceType_id = $request->input('device_type_name');
$employees->DeviceBrand_id = $request->input('device_brand_name');
$employees->DeviceStatus_id = $request->input('device_status');
$employees->save();
推荐阅读
- macos - SQL Developer 4.2.0 不会在运行 Catalina OS 的 Mac 上启动
- lua - 无法在scrapy-splash中制作Lua脚本获取页数并单击页面上的按钮
- opencv - ImportError: DLL load failed while importing cv2: 没有足够的内存资源来处理这个命令
- laravel - Laravel 8 和 Vue 3
- python - 在熊猫数据框列上执行多个正则表达式匹配
- apache-spark - 加入已经在键子集中分区的 PySpark SQL 数据帧
- trace32 - 劳特巴赫脚本:检查函数名称和参数数据类型
- python - Python创建一个只读包装类而不修改包装类
- shell - Jenkins 执行选项卡
- javascript - 使用 Javascript 获取和使用 Python 请求获取之间的关系