dre93205 2018-06-21 05:17
浏览 58
已采纳

laravel(5.5)集合的保存方法没有结果

I am trying to save several rows after processing them but when checking any data was recorded in the db

my user table contains the code column with the format "yegftwr" and "BOL234", and I need to convert all the codes with the format "yegftwr" to "BOLxxx" where xxx is an incremental number

my code first gets all the codes and then looks for the highest number xxx separating the string "BOL" (first loop)

then look for the first three characters of the "yegftwr" format codes, convert them to capital letters and compare them with the first three characters of "Bolivia" converted into capital letters if they are different then the code "yegftwr" is converted into "BOLxxx" format, xxx is an incremental number (second loop)

here my code:

  use App\User;
  use DB;

  public function handle()
{
    $tmp=User::select('codigo')->where('pais','Bolivia')->orderBy('codigo', 'asc')->get();
    $abc=strtoupper(substr('Bolivia', 0, 3));        
    foreach ($tmp as $cod)
    {
      if (strtoupper(substr($cod->codigo, 0, 3))==strtoupper(substr('Bolivia', 0, 3)))
      {
        $num=substr($cod->codigo, 3, 6);
      }
    }
      var_dump((int)$num);
      var_dump($abc.(string)(((int)$num)+1));
    foreach ($tmp as $cod)
    {
      if (strtoupper(substr($cod->codigo, 0, 3))!=strtoupper(substr('Bolivia', 0, 3)))
      {
        var_dump($cod->codigo);
        $num=(((int)$num)+1);
        $cod->codigo=$abc.(string)$num;
        $cod->save();
        var_dump($cod->codigo);
        var_dump("------------------------");
      }
    }

}

in the tests I did not get any error but no record is kept in the db

this is the output in the terminal after executing the script in the terminal

...
string(6) "ZtzBqO"
string(6) "BOL812"
string(24) "------------------------"
string(6) "zVuhyP"
string(6) "BOL813"
string(24) "------------------------"
...

but no data is stored in the db

  • 写回答

2条回答 默认 最新

  • dragonfly9527 2018-06-21 08:48
    关注

    To save the user record you should add the 'id' column to the select() method or remove the select() method.

    $tmp = User::select('id', 'codigo')->where('pais','Bolivia')->orderBy('codigo', 'asc')->get();

    or

    $tmp=User::where('pais','Bolivia')->orderBy('codigo', 'asc')->get();
    

    Code:

    use App\User;
    use DB;
    
    public function handle()
    {
        $users = User::select('id', 'codigo')->where('pais','Bolivia')->orderBy('codigo', 'asc')->get();
        $codePrefix = strtoupper(substr('Bolivia', 0, 3));        
    
        foreach ($users as $user)
        {
            if (strtoupper(substr($user->codigo, 0, 3)) == $codePrefix)
            {
                $lastCodeNumber = substr($user->codigo, 3, 6);
            }
        }
    
        foreach ($users as $user)
        {
            if (strtoupper(substr($user->codigo, 0, 3)) != $codePrefix)
            {
                $newCodeNumber = (((int)$lastCodeNumber)+1);
                $user->codigo  = $codePrefix.(string)$newCodeNumber;
                $user->save();
            }
        }
    
    }
    

    Hope it helps..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊