laravel - 列未保存在数据库中,Laravel
问题描述
我尝试在 DB 中添加值,然后获取 ID,并在另一个表中使用列,但它始终为空,我不知道为什么。
$lkp_location = new LkpLocation();
if (is_numeric($request->lkp_location_text)) { // If I get number from request, the value exists in DB, Else create and return ID
$lkp_location_id = $request->lkp_location_text;
} else {
$lkp_location->fill([
'text' => $request->lkp_location_text,
])->save();
$lastInsertedId = $lkp_location->id;
$lkp_location_id = $lastInsertedId;
}
dump($lkp_location_id); //It's the ID from DB
$person = new Person();
$person->fill([
//code with another columns...
'lkp_location_id' => $lkp_location_id,
//code with another columns...
]);
解决方案
您正在使用返回布尔值的保存方法。您需要使用 create 方法,该方法将返回您的对象。
$lkp_location->fill([
'text' => $request->lkp_location_text
])->create();
推荐阅读
- javascript - ReactJS - 使用 setInterval 减少计时器
- c++ - Qml 上下文对象在应用程序关闭时为空
- python - python:如何找到密度曲线中的最后一个最小值
- android - 从 Android 网络安全配置中引用库证书?
- html - 在 HTML 中的两列之间自动排列文本
- r - 提示类似于 AskYesNo 的问题,但值不同
- javascript - Nuxt 超出最大调用堆栈大小
- html5-video - 我可以在 html5 中删除或更改背景颜色字幕/webvtt
- git - 如何指定从远程拉取的默认分支 - 虽然我已经将一个远程配置为默认设置?
- sql - OpenRowSet 查询在来自防火墙保护的 Blob 存储的 Azure 数据库中给出错误