2017-07-24 31 views
1

我無慾無求的類別表,當我運行:Laravel查詢生成器設置ID爲0,沒有工作

DB::table('cats')->insert([ 
     'id' => 0, 
     'parent_cat_id' => null, 
     'name' => 'Uncategorized' 
    ]); 

但插入的行的編號爲1,如果我嘗試手動更新ID上分貝這是可能的。不是在查詢生成器零個工程等

任何東西(例如'id'=>5

編輯: 目前這個技巧是工作,有什麼用insert()如果update()的ID可以換到0問題?

DB::table('cats')->insert([ 
     'id' => 100, 
     'parent_cat_id' => null, 
     'name' => 'Uncategorized' 
    ]); 
    DB::table('cats')->where('id',100)->update(['id' => 0]); 

我的遷移模式:

Schema::create('cats', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->unsignedInteger('parent_cat_id')->nullable(); 
    }); 
+2

可能是你的貓表ID被定義爲unsigned –

+0

作爲@RAUSHANKUMAR所說的,請檢查您的架構類型ID字段。 –

+0

@RAUSHANKUMAR,但我可以更新到零 – Webinan

回答

0

看來你定義id柱:

$table->increments('id'); 

UNSIGNED INTEGER等同。

https://laravel.com/docs/5.4/migrations

更新

要創建自動遞增不無符號字段,這樣做:

$table->integer('id'); 
$table->primary('id'); 

口才假定主鍵是一個遞增的整數值,這意味着默認情況下主鍵會自動轉換爲int。

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

+0

是的,我想要一個增量ID。但我希望能夠插入一個0 ID,我可以直接在db中做到這一點,但爲什麼它不可能在雄辯 - >插入? – Webinan

+0

將其更改爲'$ table-> integer('id'); $ table-> primary('id');'並重新創建表。 –

+2

無符號整數可以是0 –

0

變化遷移文件。然後使用命令 - PHP工匠遷移

public function up() 
{ 
    Schema::create('table_name', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('email')->index(); 
     $table->string('token')->index(); 
     $table->timestamp('created_at'); 
    }); 
+0

我在這個問題上粘貼了我的遷移。那有什麼問題? – Webinan