php - 在控制器中传递新创建行的 ID
问题描述
如果我插入了一个新行,如下所示:
$post = new Post();
$post->save();
有没有一种直接的方法来调用上面刚刚创建的 post_id 并将其插入另一个表的新行中(而不是更改许多东西以从视图中传递它)?
$post = new Post();
$post->save();
$talk = new Talk();
$talk->post_id = // I need to put here the post_id just created. Both inserts
//are in the same controller/method
解决方案
如果$post->id
还没有,你可以试试这个
$post = new Post();
$post->save();
$post = $post->fresh();
$talk = new Talk();
$talk->post_id = $post->id;
推荐阅读
- android-studio - Android Studio 无法将大量可绘制图像复制到另一个 kotlin 项目
- node.js - 为什么我的控制器功能没有被执行?
- python - 在 Python 中更快地删除高度重叠的边界框
- python - 从 sql 响应中提取值
- visual-studio - 编译虚幻引擎源时编译时间长
- visual-studio-code - 如何获取键盘快捷键“Cmd + S”以保存在 VS Code 上?
- google-bigquery - 从结果表中删除不相关的数据 - 已编辑
- sql - Athena SQL 中的正确案例
- android - 回退到 @StringRes 的 Android 数据绑定 String 的 TextView 值
- javascript - 通过 $ 变量 javascript/html 的 href