php - 根据 id mysql laravel 5 创建 uniqueID

标签 php mysql laravel laravel-5

我想为transid列创建uniqueID,就像自动增量一样。但我已经有一列id(自动增量)。

transid应为01000001并自动增量,或遵循列id编号。

这是我的代码

public function insertFund($request,$lender, $ftype, $sign)
{
    $fmamt = str_replace(".","",$request->fmamt );
    $fdesc = $request->fdesc;
    $fdate = $request->fdate;
    $trnsfer  = $request->file('upload_trnsfer');         
    $transid = "01".str_pad($fund->id, 5, '0', STR_PAD_LEFT); 

    if($fdesc == null)
    {
        $fdesc = '';
    }
    $fund = $this->fund->create([
        'lender_id' => $lender->id,
        'transid' => $transid,
        'fmamt' => $fmamt,
        'refdc' => '',
        'ftype' => $ftype,
        'sign'  => $sign,
        'fdesc' => $fdesc,
        'fdate' => $fdate,
        'fstat' => 'pending'
    ]);
    return $fund;
}

我想,核心问题就在这里。

$transid = "01".str_pad($fund->id, 5, '0', STR_PAD_LEFT); 

我应该如何为此编写代码? 提前致谢。

最佳答案

试试这个,希望对你有帮助

$temp_str = '00000000';

最后插入的ID

$obj_fund = Fund::get()->last();
$last_inserted_id = $obj_fund->id;

$temp_index = $last_inserted_id + 1;    
$id_length = strlen($temp_index);
$temp_id = substr_replace($temp_str, $last_inserted_id, -$id_length);

你得到00000012

关于php - 根据 id mysql laravel 5 创建 uniqueID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39607590/

相关文章:

php - 全局变量与局部变量

php - 拉维尔 4 : prevent lazy load hasMany relation when foreign_key is null (optional relation)

php - 全新安装 Laravel 5.2 Make :Auth onRegister does nothing

PHP SwiftMailer Localhost 测试设置

php - 根据不同表中的数据创建时间线

php - Mysql 找到 result_id where name = "x"然后删除共享结果 id 和名称 ="y"的其他条目

PHP 只从数据库返回一个值

mysql - 选择具有 dup 值的最近项目

mysql join从3个表中获取数据,添加时间和int,将错误作为总和值的倍数

php - Laravel - 将数据库值存储到 Controller 中的变量