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