可以在一个查询中插入几个雄辩的模型吗?
假设我有一个有说服力的模型Page,并且我想在一个查询中插入页面数组或页面集合
$page1 = new Page();
$page2 = new Page();
$pages = [
$page1,
$page2,
];
or
$pages = Collection([$page1, $page2]);我想要像这样的东西
$pages->save();但是它警告说“方法save不存在”。
我看到了this,但是他们插入了数组的数组,我想插入有说服力的模型的数组。
发布于 2016-02-29 22:56:19
您可以尝试saveMany()选项。
此示例可能适用于您:
DB::table('pages')->saveMany($pages);如果你正在处理关系,你甚至可以使用一种“干净”的方式(只需向你展示如何使用这个函数的例子):
$book = App\Book::find(1);
$book->pages()->saveMany([
new App\Page(['title' => 'A new page.']),
new App\Page(['title' => 'Another page.']),
]);https://stackoverflow.com/questions/35702523
复制相似问题