Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Laravel eloquent Auto Increment?

I need an effective way to disable Laravel from auto incriminating the primary key of the table which I am going to insert data in.

Why? I don't check if there is any duplication between the DB and the inserted data so if there was any duplication I just handles it in a try-catch block.

The problem is if there was any failure Laravel counts it like I have inserted a row. So IDs column is not going to be in this order [1, 2, 3, etc], but in this [1, 4, 8, 20, etc].

I searched a lot about this issue and I have tried to use this line after the declaration of the class:

public $autoincrement = false;

Also

public $incrementing = false;

But they are not working.

I just want to use the AI of my DB. Not Laravel's one.

like image 895
Mohammed F. Ouda Avatar asked Jul 27 '17 12:07

Mohammed F. Ouda


4 Answers

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.

eg :

class UserVerification extends Model
{
    protected $primaryKey = 'your_key_name'; // or null

    public $incrementing = false;
}

in case of migration :

$table->integer('id')->unsigned(); // to remove primary key 
$table->primary('id'); //to add primary key

refer : https://laravel.com/docs/5.3/eloquent#eloquent-model-conventions

like image 195
Arun pandian M Avatar answered Nov 15 '22 18:11

Arun pandian M


Try public $incrementing = false;

like image 42
MartinTawse Avatar answered Nov 15 '22 17:11

MartinTawse


In your model:

public $incrementing = false;

In your migration:

//Remove the default $table->id();

//second param is what auto-increments, default is false so can be skipped
$table->unsignedBigInteger('id', false)->primary(); 

A quick comment on all the other outdated or wrong solutions you see here:

  • $primaryKey does not need to be overridden unless the primary column is different than 'id'
  • You do not need to use the ugly DB transaction just to remove auto-incrementing! Keep using the lovely eloquent models and just use this answer.
like image 7
usernotnull Avatar answered Nov 15 '22 17:11

usernotnull


There are 2 solutions to your problem. First one is, as you said, disable the increment column. To do that, just go to your migrations, and change

$table->increment('id)`

to

$table->integer('id')

It will remove the primary key, to set the primary key, just go to your Model file and add this:

protected $primaryKey = 'column_name';

Second solution is what I prefer. Whenever inserting, updating or removing a record and even sometimes reading a record, use laravel's DB transaction. Here is an example:

DB::beginTranscation();
try {
    $model = new Model;
    $model->column_name = $value;
    $model->save();
    DB::commit()
    return;
}
catch(exception $e) {
    DB::rollback();
    return;
}

This approach is better than remove the auto increment. But now it's upto you to choose.

like image 1
d3corator Avatar answered Nov 15 '22 17:11

d3corator