Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set id value in AUTO_INCREMENT field with laravel

Tags:

mysql

laravel

I have tried to set value id of new row in laravel 5 , by Point::create($value) and new Point($value) the $value contains a id value and this id doesn't exist in mysql db. and every time I try the new row has got a auto id not that in $value

like image 211
Mahmoud Niypoo Avatar asked Feb 12 '18 07:02

Mahmoud Niypoo


2 Answers

Add id in your fillable array in Point model class.

protected $fillable = [
    'id'
];
like image 106
Sohel0415 Avatar answered Oct 08 '22 16:10

Sohel0415


You need to set a custom primary key with:

protected $primaryKey = 'custom_key';

If you don't have PK, do this:

protected $primaryKey = null;
public $incrementing = false;

Also, remove it from fillable() array. In this case, id will be ignored.

From the docs:

Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention.

In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false. If your primary key is not an integer, you should set the protected $keyType property on your model to string.

like image 25
Alexey Mezenin Avatar answered Oct 08 '22 15:10

Alexey Mezenin