Natan Shalva
Natan Shalva

Reputation: 1572

ELOQUENT update db with save();

What i am doing wrong here ?

public static function updateuserdetails(){

   $user_id_update_user_details = Input::get();
    // find the user to update : 
   $user = User::where( 'user_id' , '=', $user_id_update_user_details['user_id'] )->first() ;

   foreach ( $user_id_update_user_details as $key => $value) {        
       $user->$key = $value ;
   }

 $affected = $user->save();
 return $affected ;
} 

It doesn't save the data to the database. I have to use "foreach" loop because i don't know what will need to be update out of all the columns.

Upvotes: 0

Views: 207

Answers (1)

Collin James
Collin James

Reputation: 9280

Set the accessible array in the model and use fill instead. Also... isn't your id called 'id'? If so setting a value for user_id will probably cause the underlying SQL to fail.

class User extends Eloquent {

    public static $accessible = array( 'id', 'name', 'email', ... );

}

Route::post( 'user/save', function ()
{
    $user = User::find( Input::get( 'id' ) );

    if ( empty( $user ) )
        return 'Could not find user';

    $user->fill( Input::get() );

    $user->save();
});

Upvotes: 1

Related Questions