Am calling function generateUniqueTinyCode() from another function create(). it returns a generated code. After execution sometimes it returns as null (in some cases) even though the variable has value. Can anyone help me?
The var_dump($this->randomCode) just before returning function has value. but after that dd($Code) is shown as null.
Note : This is a Facade ( MyTiny ).So i can call MyTiny::create('https://laravel.com/docs/5.7);
public function create( $url = null ){
if(self::validateURL($url)){
$tinyCode = $this->generateUniqueTinyCode($this->getMinLength());
dd($tinyCode);
exit;
DB::table(self::getTableName())->insert([
'short_code' => $tinyCode,
'short_url' => url($this->url_prefix.'/'.$tinyCode),
'long_url' => $url,
'created_at' => date("Y-m-d H:i:s"),
'updated_at' => date("Y-m-d H:i:s")
]);
return url($this->url_prefix.'/'.$tinyCode);
} else {
return "Please input or specify a valid URL!";
}
}
private function generateUniqueTinyCode($length = 2) {
if(config('tinyurls.USE_NUMBERS')){
$characters = '21';
} else {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
}
$charactersLength = strlen($characters);
$this->randomCode = '';
for ($i = 0; $i < $length; $i++) {
$this->randomCode .= $characters[rand(0, $charactersLength - 1)];
}
// Checks code already generated
$codeAlreadyExist = DB::table(self::getTableName())->where('short_code', '=', $this->randomCode)->exists();
if($codeAlreadyExist){
$this->generateUniqueTinyCode($this->getMinLength());
} else {
var_dump($this->randomCode);
return $this->randomCode;
}
}
OUTPUT
string(2) "21"
null
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire