作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下功能,如果一个记录不存在,它会在数据库中创建一个新记录 - 如果一个存在,它会更新它。问题是它返回 true
,因此我无法获得插入或更新记录的 ID。
/**
* Save timesheet.
*
* @param $token
* @param $data
*/
public function saveTimesheet($token, $data)
{
return $this->timesheet->firstOrNew($token)->fill($data)->save();
}
最佳答案
先创建新模型然后保存,id会自动设置到模型中。
/**
* Save timesheet.
*
* @param $token
* @param $data
*/
public function saveTimesheet($token, $data)
{
// Set the data
$model = $this->timesheet->firstOrNew($token)->fill($data);
// Save the model
$model->save();
// Return the id
return $model->id;
}
关于php - 拉维尔 5.1 : Get ID from firstOrNew method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33953308/
我正在使用 git clone 部署我的 Laravel 项目并使用 git pull 进行更新 它工作正常,但每次部署时,我都必须从 config/app.php providers 数组和 ali
我是一名优秀的程序员,十分优秀!