1

I try select from database and update values with model, but get error:

Call to undefined method stdClass::save()

My code:

$user = db::table('users')->where('name', 'John')->where('age', 30)->first();
if($user) {
    $user->name = 'Tom';
    $user->age = 31;
    $user->save();
}

i get this error:

1
  • i think db should be DB Commented Feb 9, 2015 at 13:25

1 Answer 1

5

If you want to use save() you have to build the query using an actual model. Currently you're just selecting something from the db without hydrating a model.

$user = User::where('name', 'John')->where('age', 30)->first();
if($user) {
    $user->name = 'Tom';
    $user->age = 31;
    $user->save();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.