2

I have a model which contains many methods.

class UserModel extends Eloquent{

    private $active;

    function __construct() {        
    $this->active = Config::get('app.ActiveFlag');
    }

    protected $table = 'User';
    protected $fillable = array('usr_ID', 'username');

    public function method1(){
      //use $active here
    }

    public function method2(){
      //use $active here
    }

}

Controller:

$user = new UserModel($inputall);
$user->save();

Without constructor, it works fine. However, with constructor it doesn't save the user (the query which is generated doesn't have any fill attributes or values). The query is as follows:

 insert into User() values();

Any inputs please?

2 Answers 2

4

Well yes, that's because you override the Eloquent constructor which is responsible to fill the model with values when an array is passed. You have to pass them along to the parent with parent::__construct():

public function __construct(array $attributes = array()){
    parent::__construct($attributes);
    $this->active = Config::get('app.ActiveFlag');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Missed out it completely.
1

Your model's constructor doesn't accept any parameters - empty (), and you are creating new instance of UserModel in your controller adding $inputall as a parameter.

Try to refactor your contructor according to this:

class UserModel extends Eloquent {
    public function __construct($attributes = array())  {
        parent::__construct($attributes);
        // Your additional code here
    }
}

(Answer based on other Eloquent contructor question)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.