我需要通过表格一次性存储恰好三个页面。我希望以与model save()方法类似的方式保存,因为这将自动更新记录时间戳。
如何同时处理多个记录?
我的页面模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Page extends Model{
protected $table = 'simple_pages';
}
我的代码:
public function createPages(Request $request){ // I use Page at the top
$data = [
[
'title'=> $request->first,
'content'=> $request->firstCont
],[
'title'=> $request->second,
'content'=> $request->secondCont
][
'title'=> $request->third,
'content'=> $request->thirdCont
]
];
Page::unguard();
$pages = new Page($data);
$pages->save(); // Something like this would be amazing
Page::reguard();
}
注意:我强烈反对创建多个页面模型实例,然后循环保存它们。此外,我不想使用DB插入,因为它不会自动更新记录时间戳。
Page::insert($data)
。请参考此答案:https://dev59.com/Al0a5IYBdhLWcg3w-9Da#29723968 - Soulriser