【问题标题】:How to add year to custom unique increment in Laravel 6如何在 Laravel 6 中为自定义唯一增量添加年份
【发布时间】: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   |

【问题讨论】:

    标签: php laravel laravel-6


    【解决方案1】:

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

    【讨论】:

    • 嘿,你的答案在评论//增量ID下的行中仍然有错误syntax error, unexpected 'date' (T_STRING)
    • 一分钟更新
    • 嘿,你的答案是有效的,但是在第一次输入时,或者如果我将表格设为空然后进行新输入,它会给我这个错误消息Trying to get property 'idDevice' of non-object,如果表有现有数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2021-12-21
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2018-07-30
    • 2020-10-30
    相关资源
    最近更新 更多