Search code examples
phpmysqllaravellaravel-5.3

Laravel: update a column after selecting the max id


i want to select the max id from "Users" table and update a column of that row named "status" which is initially set to "0" and after update it will be "1". how can i do that?i tried something like this

$id = DB::table('users')->InsertGetId(
      ['status' => 0]
    );              
        if($status && $id)
        {   
           DB::table('users')
           ->select max("id")
           ->update(['status' => 1]);
        }

Solution

  • You can do this:

    DB::table('users')
       ->orderBy('id', 'desc')
       ->take(1)
       ->update(['status' => 1]);