2 回答
TA贡献1805条经验 获得超10个赞
为此使用 for 循环:
foreach($pstusrmembs as $userID){
$notifs = new Notif();
$notifs->rec_uid = $userID;
$notifs->title = $post_title;
$notifs->save();
}
如果您正在使用批量分配概念
foreach ($pstusrmembs as $userID) {
Notif::create(['rec_uid' => $userID, 'title' => $post_title]);
}
如果您使用的是没有任何模型事件或侦听器的批量分配概念
foreach ($pstusrmembs as $userID) {
$arrayOfNotif[] = ['rec_uid' => $userID, 'title' => $post_title];
}
Notif::insert($arrayOfNotif);
TA贡献1963条经验 获得超6个赞
我会建议使用 DB 事务来保持数据库一致,即使在系统故障的情况下,并将该列表准备为数组并插入一行......
$prepare = [];
foreach($pstusrmembs as $p) {
$prepare[] = [
'rec_id' => $p,
'title' => $post_title
];
}
DB::beginTransaction();
try {
Notif::insert($prepare);
DB::commit();
} catch (Exception $e) {
DB::rollback();
}
- 2 回答
- 0 关注
- 148 浏览
添加回答
举报