laravel 调用未定义的方法 stdClass::save()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41051687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:54:03  来源:igfitidea点击:

Call to undefined method stdClass::save()

phplaravellaravel-5eloquentlaravel-5.2

提问by OunknownO

I'm trying to save into the database but I constantly get this error

我正在尝试保存到数据库中,但我经常收到此错误

Call to undefined method stdClass::save()

调用未定义的方法 stdClass::save()

my controller

我的控制器

 $c = DB::table('subject_user')->where('user_id', $value)->first();

 $c->auth_teacher = '1';

 $c->save();

回答by 502_Geek

Try this approach

试试这个方法

I haven't try savemethod with wherecondition. Hopefully, this approach may solve your problem.

我还没有尝试有条件的save方法where。希望这种方法可以解决您的问题。

 DB::table('subject_user')->where('user_id', $value)->update(['auth_teacher' => 1]);

回答by Ryan

I just ran into the same problem and figured out the real Laravel answer.

我刚刚遇到了同样的问题,并找出了真正的 Laravel 答案。

The answer you've accepted here (https://stackoverflow.com/a/41051946/470749) doesn't follow the truest Laravel way.

您在此处接受的答案 ( https://stackoverflow.com/a/41051946/470749) 并未遵循最真实的 Laravel 方式。

When I re-read the docs at https://laravel.com/docs/5.4/eloquent#retrieving-models, I noticed that ->save()will work only if the way that I retrievedthe object(s) was via the model like this:

当我在https://laravel.com/docs/5.4/eloquent#retrieving-models重新阅读文档时,我注意到->save()只有当我检索对象的方式是通过这样的模型时才会起作用:

$flights = App\Flight::where('active', 1)
           ->orderBy('name', 'desc')
           ->take(10)
           ->get();

There is no need to use DB::table()anywhere, and using it doesn't seem to be the recommended way.

没有必要在DB::table()任何地方使用,使用它似乎不是推荐的方式。

So, you could change DB::table('subject_user')->whereto App\Subject_user::where(or whatever is appropriate for you).

因此,您可以更改DB::table('subject_user')->whereApp\Subject_user::where(或任何适合您的)。

回答by Chris8447

As you can read here there are two ways of saving/updating records. In your case you would need to do the following: https://laravel.com/docs/6.x/queries#updates

正如您在此处阅读的那样,有两种保存/更新记录的方法。在您的情况下,您需要执行以下操作:https: //laravel.com/docs/6.x/queries#updates

$affected = DB::table('users')
              ->where('id', 1)
              ->update(['votes' => 1]); 

回答by OunknownO

DB::table('subject_user')->where('user_id', $value)->update(['auth_teacher' => 1]);

DB::table('subject_user')->where('user_id', $value)->update(['auth_teacher' => 1]);

I have been able to succeed with this.

我已经能够成功了。