Laravel 如何更新行和返回id?
我使用此函数ORM更新表中的行: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 您应该在变量中分配
$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!