PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] Laravel 5.8 Eloquent Create() return wrong Id

 January 24, 2022     eloquent, laravel, laravel-5, laravel-5.8, php     No comments   

Issue

public function up() {
        Schema::create( 'active_stocks', function ( Blueprint $table ) {
            $table->bigIncrements( 'id' );
            $table->string( 'sku' );
            $table->text( 'title' )->nullable()->default( '' );
            $table->integer( 'cost' )->default(0);
            $table->integer( 'qty' )->default(0);
            $table->string( 'stock_tracking' )->default('')->nullable();
            $table->integer( 'type' )->default(1);
            $table->text( 'image' )->nullable();
            $table->integer( 'user_id' );
            $table->integer( 'status' )->default(1);
            $table->timestamps();
        } );
        \Illuminate\Support\Facades\DB::statement( "ALTER TABLE `active_stocks` AUTO_INCREMENT = 9999999999;" );
    }

I have set auto increment as some value that i wanted in development. Then i create a new record using Laravel Eloquent

$create = App/ActiveStock::create(
 [
    'sku'            => $item_array['sku'],
    'qty'            => $item_array['qty'],
    'stock_tracking' => $item_array['tracking'],
    'type'           => ActiveStock::TYPE_IMPORTED,
    'cost'           => $item_array['cost'],
    'user_id'        => $user_id,
 ]
)

echo $create->id; //Should be 9999999999

but i got this 2147483647 But i looked on database the id value is 9999999999 which is right.

What i missing here.

this is the full response of $create

App\ActiveStock Object
(
    [fillable:protected] => Array
        (
            [0] => sku
            [1] => title
            [2] => cost
            [3] => qty
            [4] => stock_tracking
            [5] => type
            [6] => image
            [7] => user_id
            [8] => status
        )

    [connection:protected] => mysql
    [table:protected] => active_stocks
    [primaryKey:protected] => id
    [keyType:protected] => int
    [incrementing] => 1
    [with:protected] => Array
        (
        )

    [withCount:protected] => Array
        (
        )

    [perPage:protected] => 15
    [exists] => 1
    [wasRecentlyCreated] => 1
    [attributes:protected] => Array
        (
            [sku] => test-B00MSOIUOO
            [qty] => 11
            [stock_tracking] => 
            [type] => 1
            [cost] => 112
            [user_id] => 1
            [updated_at] => 2020-02-08 17:12:38
            [created_at] => 2020-02-08 17:12:38
            [id] => 2147483647
        )

    [original:protected] => Array
        (
            [sku] => test-B00MSOIUOO
            [qty] => 11
            [stock_tracking] => 
            [type] => 1
            [cost] => 112
            [user_id] => 1
            [updated_at] => 2020-02-08 17:12:38
            [created_at] => 2020-02-08 17:12:38
            [id] => 2147483647
        )

    [changes:protected] => Array
        (
        )

    [casts:protected] => Array
        (
        )

    [dates:protected] => Array
        (
        )

    [dateFormat:protected] => 
    [appends:protected] => Array
        (
        )

    [dispatchesEvents:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [relations:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [timestamps] => 1
    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

)

Solution

2147483647 is the maximum value of 32-bit signed integers. You are probably using a 32-bit version of PHP.



Answered By - Olivier
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing