Search code examples
laravellaravel-5.3laravel-5.4

How to update row and return id?


I use this function ORM to update row in table:

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

How to return id of updated row?


Solution

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

    You should assign the query result in a variable, process the update and then access the 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