Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用未定义的方法stdClass::save()_Php_Laravel_Laravel 5_Eloquent_Laravel 5.2 - Fatal编程技术网

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

Php 调用未定义的方法stdClass::save(),php,laravel,laravel-5,eloquent,laravel-5.2,Php,Laravel,Laravel 5,Eloquent,Laravel 5.2,我试图保存到数据库中,但我经常遇到这个错误 调用未定义的方法stdClass::save() 我的控制器 $c = DB::table('subject_user')->where('user_id', $value)->first(); $c->auth_teacher = '1'; $c->save(); DB::table('subject\u user')->其中('user\u id',$value)->更新(['auth\u teacher'=>1

我试图保存到数据库中,但我经常遇到这个错误

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

我的控制器

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

 $c->auth_teacher = '1';

 $c->save();
DB::table('subject\u user')->其中('user\u id',$value)->更新(['auth\u teacher'=>1])

我已经成功地做到了这一点

试试这种方法

我没有使用
where
条件尝试
save
方法。希望这种方法能解决您的问题

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

我只是遇到了同样的问题,并找到了真正的拉威尔答案

你在这里接受的答案()并没有遵循最真实的拉威尔方式

当我在上重新阅读文档时,我注意到只有通过以下模型检索对象时,
->save()
才会起作用:

$flights = App\Flight::where('active', 1)
           ->orderBy('name', 'desc')
           ->take(10)
           ->get();
无需在任何地方使用
DB::table()
,使用它似乎不是推荐的方法


因此,您可以将
DB::table('subject\u user')->where
更改为
App\subject\u user::where
(或任何适合您的内容)。

正如您在这里看到的,有两种保存/更新记录的方法。在您的情况下,您需要执行以下操作:


在某些情况下,Laravel控制器不接受find方法。也许你试过你的控制器

解决方案-1)
尝试在模型中使用查找方法

解决方案-2)

尝试在Controller中使用Where子句,而不是find。

如果您开始使用DB::而不是Model::因为您需要动态表名调用,那么可以使用动态接口调用,但我想知道这是否是一种好的做法,是否有人愿意解释以下内容是否错误(因为它工作正常):


您是否有为subject_用户表创建的模型?DB::table方法不会返回具有save方法的对象。不,我不会返回,因为这是一个透视表。我将直接转到iTunes。如果您没有独特的场景,我将尝试使用模型执行此操作。看起来您在多对多关系上有轴心值。下面的堆栈线程对您有帮助吗。您的代码可能如下所示$user->subjects()->updateExistingPivot($subject->id,数组('auth\u teacher'=>1),false);这是一个更合适的答案。你似乎无法在雄辩的模型中使用悲观锁。如果需要防止竞争条件,则必须使用DB::table()。这对我也很有帮助:
$affected = DB::table('users')
              ->where('id', 1)
              ->update(['votes' => 1]); 
$model = 'User'; //Use the name of the model, not the table name
$interface_model = '\App\Models\\' . $model; //Needed to avoid the manual import of each model
$entry = $interface_model::where('id', $id)->first();
//$entry now support save()