gpt4 book ai didi

mysql - Laravel Eloquent - 一起插入到相关表中

转载 作者:行者123 更新时间:2023-11-30 22:27:54 26 4
gpt4 key购买 nike

我搜索了这个并得到了很多结果,每个人都说它有效。

我的情况和这里显示的完全一样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()
for $student->device()->create(['token' => $token]);

最佳答案

知道了!

 $post = (new Student)->fill(['name'=>'robin'])
->device()->associate(Device::create(['token' => 'TOKEN']))
->save();

引用:laravel Eloquent ORM multiple table insert

关于mysql - Laravel Eloquent - 一起插入到相关表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34719298/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com