【发布时间】:2019-11-12 15:47:45
【问题描述】:
我在我的控制器中使用的程序中有一个自定义的唯一增量,因此每个项目不会有相同的标识符
控制器
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 |
【问题讨论】: