How to update row and return id?

11,576

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
Share:
11,576
OPV
Author by

OPV

Updated on June 14, 2022

Comments

  • OPV
    OPV almost 2 years

    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?

  • Disfigure
    Disfigure over 6 years
    that means no record has been found in your database with your where clause
  • parker_codes
    parker_codes over 6 years
    You can prevent errors by checking if client is set before updating.
  • Alauddin Ahmed
    Alauddin Ahmed over 4 years
    This is not working if there is multiple records. Do have any way around to get the ids without using another query!