Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined method stdClass::save()

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

Call to undefined method stdClass::save()

my controller

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

 $c->auth_teacher = '1';

 $c->save();
like image 246
OunknownO Avatar asked Dec 09 '16 01:12

OunknownO


3 Answers

Try this approach

I haven't try save method with where condition. Hopefully, this approach may solve your problem.

 DB::table('subject_user')->where('user_id', $value)->update(['auth_teacher' => 1]);
like image 94
502_Geek Avatar answered Oct 18 '22 12:10

502_Geek


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

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

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 retrieved the object(s) was via the model like this:

$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.

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

like image 11
Ryan Avatar answered Oct 18 '22 11:10

Ryan


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

I have been able to succeed with this.

like image 1
OunknownO Avatar answered Oct 18 '22 10:10

OunknownO