Laravel Eloquent resets primary key value after save() called -


i'm using laravel 5.2 , when insert model, primary key id column somehow reset 0. what's happening below.

$foomodel = new foomodel(); $foomodel->foo_id = 123; $foomodel->foo_title = 'foo'; echo($foomodel->foo_id); //123 $foomodel->save(); echo($foomodel->foo_id); //0 

the data inserted on mysql database, foo_id column reset 0 in php. if it's autoincrement column, know it'll updated next sequence value, column not. there i'm missing?

class foomodel extends model {     protected $table = 'foo_table';     protected $primarykey = 'foo_id';     protected $guarded = ['foo_id'];     protected $casts = [         'foo_id' => 'integer'     ]; } 

problem solved. needed declare "public $incrementing = false;" in model class.

class foomodel extends model {     protected $table = 'foo_table';     protected $primarykey = 'foo_id';     public $incrementing = false;     protected $guarded = ['foo_id'];     protected $casts = [         'foo_id' => 'integer'     ]; } 

the default setting $incrementing true. that's why laravel set id column 0.

abstract class model {      /**      * indicates if ids auto-incrementing.      *      * @var bool      */     public $incrementing = true;  } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -