laravel 如何更新行并返回ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46373308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:42:14  来源:igfitidea点击:

How to update row and return id?

laravellaravel-5.3laravel-5.4

提问by OPV

I use this function ORM to update row in table:

我使用这个函数 ORM 来更新表中的行:

$client = Client::where("unique_code", $this->unique_code)->whereHas('types', function ($query) {
  $query->where("type", $this->type);
})->update(["status" => 1]);

How to return idof updated row?

如何返回id更新的行?

回答by Disfigure

First what your using is not the ORM but the QueryBuilder.

首先,您使用的不是 ORM,而是 QueryBuilder。

You should assign the query result in a variable, process the update and then access the id.

您应该将查询结果分配到一个变量中,处理更新,然后访问 id。

$client = Client::where("unique_code", $this->unique_code)
  ->whereHas('types', function ($query) {
      $query->where("type", $this->type);
  })->first();

$client->update(["status" => 1]);
$client->id; // to get the id