首页 > 解决方案 > 如何在 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   |

标签: phplaravellaravel-6

解决方案


拳头从数据库中获取最新记录并根据年份增加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();

推荐阅读