如何更新行和 return id?

How to update row and return id?

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

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

如何return id 更新行?

首先你使用的不是ORM而是QueryBuilder。

您应该将查询结果分配到一个变量中,处理更新,然后访问 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