0

Is it possible to use sessions in eloquent model

class StructureModel extends Eloquent {


  protected $table = 'page';        


  protected $attributes = array('lng' => Session::get("lngFunction")); 
}       

Any help?

2
  • 3
    I'm not sure, never tried it. But if it doesn't work you can always pass session values as parameters to your model instance (like $model->some_value = Session::get("lngFunction");). Commented Dec 26, 2013 at 16:07
  • Hard-coding parameters like these are not recommended. As Andre recommended, it's best to pass these to the model on a case-by-case basis. If you hardcode the session and change it's name later, you're going to regret the back-tracking you have to do to solve your problem. :) Commented Dec 26, 2013 at 16:16

1 Answer 1

2

This is not a Laravel thing, you simple cannot define variables this way in PHP, this is not how PHP works, but you use the construct method:

class StructureModel extends Eloquent {

    protected $table = 'page';        

    protected $attributes = array();

    public function __construct()
    {
         parent::__construct();

         $this->attributes = array('lng' => Session::get("lngFunction"));
    }
}
Sign up to request clarification or add additional context in comments.

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.