Laravel bigInteger being rounded to int in relationship

前端 未结 1 439
走了就别回头了
走了就别回头了 2021-01-16 22:55

Alright, so here\'s my migrations...

public function up()
{
    Schema::create(\'instagrams\', function (Blueprint $table) {
        $table->bigInteger(\'         


        
相关标签:
1条回答
  • 2021-01-16 23:38

    It sounds like you're using a 32-bit version of PHP, where the max integer value is 2147483647.

    The issue is that when the relationship query gets the key value of the Instagram instance to query the users, it automatically casts that id value to the type defined by the $keyType property on the model. This property is int by default.

    So, even though your Instagram instance id is "3620243170", it is cast to an int, which in 32-bit PHP will turn it into 2147483647.

    There are a couple things you can try to mitigate this issue:

    1. Use a 64-bit version of PHP. The max int size for 64-bit PHP matches the max int available for a signed bigint field. However, if you're using an unsigned bigint, you will run into this issue again once your ids exceed 9223372036854775807 (not likely).

    2. Change the $keyType property on your Instagram model to float, or possibly string. This only affects Eloquent's casting of the variables in PHP, it does not affect how they are stored in the database.
      Add protected $keyType = 'float'; to your Instagram model.

    0 讨论(0)
提交回复
热议问题