gpt4 book ai didi

php - 拉维尔 5.1 : Get ID from firstOrNew method

转载 作者:可可西里 更新时间:2023-10-31 22:41:34 24 4
gpt4 key购买 nike

我有以下功能,如果一个记录不存在,它会在数据库中创建一个新记录 - 如果一个存在,它会更新它。问题是它返回 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com