简体   繁体   中英

Laravel SQL select and update model

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM