2016-01-11 113 views
1

我搜索了這個,並得到了很多結果,並且大家都說它的工作原理。Laravel雄辯 - 一起插入相關表

我的情況是完全一樣的,如下所示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]);

回答