Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如何更新行和返回id?_Laravel_Laravel 5.3_Laravel 5.4 - Fatal编程技术网

Laravel 如何更新行和返回id?

Laravel 如何更新行和返回id?,laravel,laravel-5.3,laravel-5.4,Laravel,Laravel 5.3,Laravel 5.4,我使用此函数ORM更新表中的行: $client = Client::where("unique_code", $this->unique_code)->whereHas('types', function ($query) { $query->where("type", $this->type); })->update(["status" => 1]); 如何返回已更新行的id?首先,您使用的不是ORM,而是QueryBuilder 您应该在变量中分配

我使用此函数ORM更新表中的行:

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

如何返回已更新行的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

首先,您使用的不是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

这意味着在数据库中找不到包含where子句的记录。您可以通过在更新之前检查客户端是否已设置来防止错误。如果存在多条记录,则此操作无效。你有没有办法不用另一个查询就获得ID!这意味着在数据库中找不到包含where子句的记录。您可以通过在更新之前检查客户端是否已设置来防止错误。如果存在多条记录,则此操作无效。你有没有办法不用另一个查询就获得ID!