xx7xx7xx
xx7xx7xx

Reputation: 113

Laravel Get Record ID from Update Query

For this update query, I'm trying to get the id after I run it.

$results = DB::table('testDB123.users')
    ->where('fbID', '=', $x['id'])
    ->update([
            'updated_at' => $x['currDate'],
            'fbFirstName' => $x['firstName'],
            'fbLastName' => $x['lastName']
        ]
    );

Tried this with no luck $results->id

Is there anything similar to insertGetId for update queries?

$id = DB::table($table1)->insertGetId([...])

Upvotes: 6

Views: 15494

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

update() method doesn't return an object, so you have two options:

Option 1

Use updateOrCreate():

$user = User::updateOrCreate(['fbID' => $x['id']], $dataArray);
$id = $user->id;

Option 2

Get an object and update it:

$user = User::where('fbID', '=', $x['id'])->first();
$user->update($dataArray);

$id = $user->id;

Upvotes: 10

Related Questions