laravel5.2 create()で作ったのに戻り値に主キーがセットされない
こんなmigration
<?php /** * Run the migrations. * * @return void */ public function up() { // Schema::create('temps', function (Blueprint $table) { $table->string('id'); $table->string('name'); $table->timestamps(); $table->primary('id'); }); }
こんなModel
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Temp extends Model { protected $fillable = ['id', 'name']; public function newEntry($data) { $authUser = $this->where('id', $data['id'])->first(); if ($authUser){ return $authUser; } return $this->create([ 'id' => $data['id'], 'name' => $data['name'], ]); } }
で、戻り値みると、 id には 0 が入ってきます。 テーブルにはちゃんと入ってる。だから2回目叩くと取れる。
数時間後
あった。 phpstormでステップ実行しながら見つけた…。
if ($this->incrementing ) {
//
} else {
//
}
なんかこれっぽいだろ…
その後ぐぐったらヒット。
public $incrementing = false;
Modelにこれを追加だって。