我是laravel的新手,试图通过一个控制器的api将数据存储到多个表中

时间:2018-06-20 09:16:52

标签: laravel eloquent

将有四个表,其中第一个表ID是主键,而其他表则是外键。如何插入数据。 数据应仅与第一个表ID相关。

1 个答案:

答案 0 :(得分:0)

您可以通过检索存储的ligne的id(主键)来管理它。就是这样

$primId = PrimModel::create([$datas]);
/* where datas are the informations you want to store
eg : $primId = PrimModel::create(['arg1' => 'val1', 'arg2'=> $val2, ...]); */
//Now you can store the others like that
ForeignModel::create(['primId' => $primId->id, ...]);
...

希望这可以为您提供帮助。