Laravel 5, Call to undefined method stdClass::update()

Mahmood Kohansal picture Mahmood Kohansal · Feb 2, 2016 · Viewed 10.9k times · Source

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

FatalErrorException in UserController.php line 47: Call to undefined method 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?

Answer

Imtiaz Pabel picture Imtiaz Pabel · Feb 2, 2016

you can't use find method in query builder,you need to use 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');
}           
}