Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 Laravel 5,调用未定义的方法stdClass::update()_Php_Laravel_Laravel 5_Stdclass_Laravel Query Builder - Fatal编程技术网

Php Laravel 5,调用未定义的方法stdClass::update()

Php Laravel 5,调用未定义的方法stdClass::update(),php,laravel,laravel-5,stdclass,laravel-query-builder,Php,Laravel,Laravel 5,Stdclass,Laravel Query Builder,我使用的是Laravel5,我有一个简单的编辑页面,必须更新数据库。当我试着运行它时,我发现了一个错误 UserController.php第47行中的FatalErrorException:调用未定义的方法stdClass::update() 控制器: public function userUpdate() { if(Input::get('send')) { $arr = [ 'email' => Input::get('email')

我使用的是Laravel5,我有一个简单的编辑页面,必须更新数据库。当我试着运行它时,我发现了一个错误

UserController.php第47行中的FatalErrorException:调用未定义的方法stdClass::update()

控制器:

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');
    }           
}
数据库架构:

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::post('/user/update', [
    'as' => 'userUpdate', 'uses' => 'UserController@userUpdate'
]);
{!! 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() !!}
路线:

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::post('/user/update', [
    'as' => 'userUpdate', 'uses' => 'UserController@userUpdate'
]);
{!! 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() !!}
查看:

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::post('/user/update', [
    'as' => 'userUpdate', 'uses' => 'UserController@userUpdate'
]);
{!! 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() !!}
{!!Form::open(['route'=>'userUpdate','method'=>'post'])
用户名
{!!Form::text('username',$user->username,['class'=>'表单控件只读','readonly'=>'')
电子邮件
{!!Form::text('email',$user->email,['class'=>'Form control'])
许可
{!!Form::text('permission',$user->permission,['class'=>'Form control'])
{!!Form::hidden('userId',$user->id)
{!!Form::submit('Update User',['class'=>'btn green','name'=>'send'])
{!!表单::提交('Cancel',['class'=>'btn black','name'=>'clear'])
{!!Form::close()!!}

我使用这种结构,特别是在我的控制器中的另一个函数中使用Input和Form类来生成行列表或获取表的单个行,一切正常,但在更新数据库时,我得到了对未定义方法stdClass::update()的调用。错误。我缺少什么吗?

您不能在查询生成器中使用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');
}           
}

就我而言,我不得不从

first() to limit(1)