简体   繁体   中英

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

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

You are fetching data where 'pais' is 'Bolivia'. Result will display all data with Boliva. And in second foreach loop you are checking $cod->codigo is not equal to 'Bolivia'. 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.

$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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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