Home > Net >  Laravel records entity wrong
Laravel records entity wrong

Time:10-06

There are entity and on recording to database, record wrong data

Code to record entity

public function createTelegramUser($entity)
{                                                   
    TelegramUser::create($entity);
}

Entity

[entity] => Array
    (
        [telegram_id] => 5403205983
        [is_bot] => 
        [first_name] => Belarus
        [username] => dilshodbelarus
        [language_code] => ru
        [message] => /start
        [context] => 
    )

Database record

enter image description here

Look at telegram_id, on entity 5403205983 but on database 1108238687

Details:

Table telegram_users structure

enter image description here

field telegram_id is varchar(255)

Model TelegramUser

<?php
namespace App\Models\API;
use App\Traits\TraitUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TelegramUser extends Model
{
    use HasFactory, TraitUuid;
    protected $fillable = ['telegram_id', 'is_bot', 'first_name', 'last_name', 'username', 'language_code', 'status'];
    protected $attributes = ['status' => 1];
}

TraitUuid

<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait TraitUuid
{
/**
 * Override the boot function from Laravel so that
 * we give the model a new UUID when we create it.
 */
protected static function boot()
{
    parent::boot();

    $creationCallback = function ($model) {
        if (empty($model->{$model->getKeyName()}))
        {
            $model->{$model->getKeyName()} = Str::uuid()->toString();
        }
    };

    static::creating($creationCallback);
}

/**
 * Tell laravel that the key name is a uuid, not an id.
 *
 * @return string
 */
public function getKeyName(): string
{
    return 'uuid';
}


/**
 * Override the getIncrementing() function to return false to tell
 * Laravel that the identifier does not auto increment (it's a string).
 *
 * @return bool
 */
public function getIncrementing() : bool
{
    return false;
}


/**
 * Tell laravel that the key type is a string, not an integer.
 *
 * @return string
 */
public function getKeyType() : string
{
    return 'string';
}
}

Data came from telegram webhook, Entity dump

$request = file_get_contents('php://input');
$request = json_decode($request, TRUE);
$message = $request['message'];
$entity = [
        'telegram_id' => $message['from']['id'],
        'is_bot' => $message['from']['is_bot'],
        'first_name' => $message['from']['first_name'],
        'username' => $message['from']['username'],
        'language_code' => $message['from']['language_code'],
        'message' => $msg['msg'],
        'context' => $msg['context'],
    ];
TelegramUser::create($entity);

CodePudding user response:

Thanks everyone for response, problem was in memory limit, in my php.ini file it was 64MB, thats how number is long this why it sets how many bytes does not enough.

there are two solution change php.ini

ini_set('memory_limit', '1024M');

or covert integer to string

'telegram_id' => (string) $message['from']['id'],

CodePudding user response:

You have 100% an overflow issue. Ironic, because this is stack-overflow

Max int in mysql = 2147483647

Your input = 5403205983

Your result = 1108238687

If you add 1 to 2147483647, it will become -2147483647 if you try to store 5403205983, it will result in:

5403205983-(2147483647*2) = 110823869(the fact that it is off by 2 is probably me overlooking something here, but it is too similar to ignore)

You say you made the column a string. I think you did add it to the migration, but did not migrate it. Please make the column type bigInt or string, verify the column is actually bigInt or string, and retry your code :)

CodePudding user response:

For use cases like this you could use bigInt i don't see a point to be a text record if u expect only numeric values (maybe i'm wrong but on these examples it is all numeric). Or you could pass how long data should be upon creating records in database like:

$table->bigInt('telegram_id',10)->unsigned()->unique();
  • Related