1

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()]);

2 Answers2

5

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

Quezler
  • 2,376
  • 14
  • 29
2

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.

Chintan7027
  • 7,115
  • 8
  • 36
  • 50