Laravel 5,调用未定义的方法 stdClass::update()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35146650/
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
Laravel 5, Call to undefined method stdClass::update()
提问by Mahmood Kohansal
I'm using Laravel 5 and I have a simple edit page that must update database. when I try to run it, I got error
我正在使用 Laravel 5 并且我有一个必须更新数据库的简单编辑页面。当我尝试运行它时,出现错误
FatalErrorException in UserController.php line 47: Call to undefined method stdClass::update()
UserController.php 第 47 行中的 FatalErrorException:调用未定义的方法 stdClass::update()
Controller :
控制器 :
public function userUpdate()
{
if(Input::get('send')) {
$arr = [
'email' => Input::get('email')
];
DB::table(Config::get('users_table'))->find(Input::get('userId'))->update(['is_active' => TRUE]);
return redirect()->route('users');
}
}
Database schema :
数据库架构:
id bigserial NOT NULL,
username text,
email text,
permission integer NOT NULL,
is_staff boolean NOT NULL DEFAULT false,
is_active boolean NOT NULL DEFAULT false,
updated_at timestamp without time zone,
created_at timestamp without time zone,
remember_token text
Route :
路线 :
Route::post('/user/update', [
'as' => 'userUpdate', 'uses' => 'UserController@userUpdate'
]);
View :
看法 :
{!! Form::open(['route' => 'userUpdate', 'method' => 'post']) !!}
<label>Username</label>
{!! Form::text('username', $user->username, ['class' => 'form-control readonly', 'readonly' => '']) !!}
<label>Email</label>
{!! Form::text('email', $user->email, ['class' => 'form-control']) !!}
<label>Permission</label>
{!! Form::text('permission', $user->permission, ['class' => 'form-control']) !!}
{!! Form::hidden('userId', $user->id) !!}
{!! Form::submit('Update User', ['class' => 'btn green', 'name' => 'send']) !!}
{!! Form::submit('Cancel', ['class' => 'btn black', 'name' => 'clear']) !!}
{!! Form::close() !!}
I use this structure, specially this type of using Input and Form class in another function in my controller for make list of rows or get an individual row of table, and everything was ok, but in updating database I got Call to undefined method stdClass::update()
error. Is there something that I am missing?
我使用这种结构,特别是在我的控制器的另一个函数中使用 Input 和 Form 类的这种类型,用于制作行列表或获取表的单个行,一切正常,但在更新数据库时出现Call to undefined method stdClass::update()
错误。有什么我想念的吗?
回答by Imtiaz Pabel
you can't use find method in query builder,you need to use where
您不能在查询构建器中使用 find 方法,您需要使用 where
public function userUpdate()
{
if(Input::get('send')) {
$arr = [
'email' => Input::get('email')
];
DB::table(Config::get('users_table'))->where('id',Input::get('userId'))->update(['is_active' => TRUE]);
return redirect()->route('users');
}
}
回答by Black Mamba
In my case I had to change from
在我的情况下,我不得不改变
first() to limit(1)
ie.
IE。
$blog = DB::table('blogs')
->where('blogs.id', $id)
->first();
To
到
$blog = DB::table('blogs')
->where('blogs.id', $id)
->limit(1);