我搜索了这个并得到了很多结果,每个人都说它有效。
我的情况与此处http://laravel.io/forum/04-22-2015-hasone-and-insert
完全相同Schema::create('devices', function(Blueprint $table)
{
$table->increments('id');
$table->string('token')->unique();
$table->enum('type', ['android', 'ios']);
$table->timestamps();
});
Schema::create('students', function(Blueprint $table)
{
$table->increments('id');
$table->string('nom', 15);
$table->integer('device_id')->unsigned();
$table->timestamps();
$table->foreign('device_id')->references('id')->on('devices')->onDelete('cascade');
});
学生模特
public function device() {
return $this->hasOne('App\Device');
}
设备型号
public function user()
{
return $this->belongsTo('App\Etudiant');
}
我试过
$student = Student::create(['nom' => $nom, 'type' => $type]);
$student->device()->create(['token' => $token]);
$student->save();
但我收到Call to undefined method Illuminate\Database\Query\Builder::create()
$student->device()->create(['token' => $token]);
答案 0 :(得分:0)
知道了!
$post = (new Student)->fill(['name'=>'robin'])
->device()->associate(Device::create(['token' => 'TOKEN']))
->save();