简体   繁体   English

laravel(5.5)的保存方法中没有任何结果

[英]laravel(5.5) there are no results in save method for a collection

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 我的用户表包含格式为“ yegftwr”和“ BOL234”的代码列,我需要将所有格式为“ yegftwr”的代码都转换为“ BOLxxx”,其中xxx是一个递增数字

my code first gets all the codes and then looks for the highest number xxx separating the string "BOL" (first loop) 我的代码首先获取所有代码,然后查找分隔字符串“ BOL”的最高数字xxx(第一个循环)

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) 然后查找“ yegftwr”格式代码的前三个字符,将它们转换为大写字母,并将它们与转换为大写字母的“玻利维亚”的前三个字符进行比较(如果它们不同),然后将代码“ yegftwr”转换为“ BOLxxx“格式,xxx为递增数字(第二个循环)

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 但是没有数据存储在数据库中

You are fetching data where 'pais' is 'Bolivia'. 您正在获取“ pais”为“玻利维亚”的数据。 Result will display all data with Boliva. 结果将显示Boliva的所有数据。 And in second foreach loop you are checking $cod->codigo is not equal to 'Bolivia'. 在第二个foreach循环中,您正在检查$ cod-> codigo不等于“玻利维亚”。 Check your second if condition, is this should be equal to or not equal to 检查您的第二个条件是否等于或不等于

To save the user record you should add the ' id ' column to the select() method or remove the select() method. 要保存用户记录,您应该将“ id ”列添加到select()方法或删除select()方法。

$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.. 希望能帮助到你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM