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
Post a Comment