Creating a default object from empty value while updating user profile record

51 Views Asked by At

I want to update user profile record, but while submitting, an error popped up with a message "Creating default object from empty value", when i remove if statements,and type only var_dump($data),,, it is running smoothly, and show all data, but in the presence of if commands, error popped up. i don't know, why it's happening, any help please,

class home extends Model
{

    protected $table="homes";
    public static function upstore($data){
    $firstname=Input::get('fname');
    $lastname=Input::get('lname');
    $phone=Input::get('phone');
    $address=Input::get('address');
    $profileimage=Input::get('pimage');
    $password=Input::get('pswrd');
    $confirmpassword=Input::get('cpswrd');

      if( $firstname != '' ){
        $homes->fname    = $firstname;
      }
     if( $lastname != '' ){
        $homes->lname    = $lastname;
      }
     if( $phone != '' ){
        $homes->phone    = $phone;
      }
     if( $address != '' ){
        $homes->address    = $address;
      }
     if( $profileimage != '' ){
        $homes->pimage    = $profileimage;
      }
     if( $password != '' ){
        $homes->pswrd    = $password;
      }
     if( $confirmpassword != '' ){
        $homes->cpswrd    = $confirmpassword;
      }

         $homes->save();
     }
  }

It shows this error

Creating a default object from empty value

2

There are 2 best solutions below

3
nakov On

After the first if statement you try to set a property to a value but you never initialized the $homes to any object.

So above the if statements at this:

$homes = new home;
0
Benjamin Beganović On

As I can see, you have @upstore method in your model. I can see a few issues here:

  • For what exactly do you use controllers? For displaying views only? Controllers are part of the application that turns input request to the output request, so please switch this logic into the controller, because if you keep this convention for yourself you'll easily grab some troubles when you get the job.

  • if ($user, $address..) - why don't make use of Laravel Validator? =)

  • A proper way of your != '' is: (Request::has('field')

Don't think I am saying this just because I want to look smarter, but these are mistakes which will make your code declined instantly when you start working in the team.

Back to the answer. I believe if you fix all this stuff, the code should work just fine.