Laravel 5.5 Call to undefined method stdClass::update()

Laravel 5.5 show me the error message:

Call to undefined method stdClass::update()

for the following codes in the Model

DB::table('adminUserLogs')
            ->where('adminUserId', $id)
            ->where('adminUserOutTime', NULL)->first()
            ->update(['adminUserOutTime' => \Carbon\Carbon::now()]);

Upvotes: 1

Views: 6829

Answers (2)

Chintan7027
Chintan7027

Reputation: 7615

In Laravel if you wish to update only one record then you can use ->first() method of Eloquent ORM to get eloquent object and then you can simply update the object by ->save().

Laravel query builder directly not supporting update only record as Update with limit of 1 is not directly supported by mysql MySQL - UPDATE query with LIMIT

In the example you have given here is some modification to be work,

DB::table('adminUserLogs')
        ->where('adminUserId', $id)
        ->where('adminUserOutTime', NULL)
        ->update(['adminUserOutTime' => \Carbon\Carbon::now()]);

This will result in updating the records where specified conditions. To update specific record pass id column in where clause of that particular record.

Upvotes: 2

Quezler
Quezler

Reputation: 2435

Try using ->limit(1) instead of ->first() in the chain.

Upvotes: 5

Related Questions