Tinabot
Tinabot

Reputation: 429

Laravel primary key incrementation in eloquent and migration

So I have custom primary keys and I am not sure If I need to define the incrementation in both eloquent and migrations?

Eloquent

class Question extends Model 
{
protected $primaryKey = 'que_id';
protected $keyType = 'tinyInteger'; 
public $autoincrement = true;
}

Migration

public function up()
    {
        Schema::create('questions', function (Blueprint $table) {
        $table->tinyInteger('que_id')->autoIncrement();
        $table->timestamps();
        });
    }

Or it is just enough to define it like this?

public function up()
    {
        Schema::create('questions', function (Blueprint $table) {
        $table->tinyInteger('que_id')->primary();
        $table->timestamps();
        });
    }

Upvotes: 1

Views: 3876

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25936

Model:

  • $keyType supports int[eger] and string
  • There is no $autoincrement property, it's called $incrementing.

Migration:

  • The second parameter of tinyInteger() etc. is $autoIncrement = false.
  • If you mark a column as $autoIncrement, you don't need and can't use ->primary() (at least on MySQL).

Upvotes: 1

Related Questions