class Book extends Model
{
private $var = 1;
public function afterFetch()
{
if ($this->var == 2) $this->doSomething();
}
public function setVar($val)
{
$this->var = $val;
}
public function doSomething(){}
}
$book = Book::find(1);
Question: how can I, in the above scenario, get $this->var to be equal to 2 in afterFetch() ?
if you want it before afterfetch() method then you have to use some other method like initialize() and put your snippet in there, but that would be same as just having private $var = 2; on top fo your model code.
If you want to dynamicaly change your variable before fetching the data from database then you have to (just look at Brian's code above this post) create new model and set your var from outside and then run find().
Just make sure that your var doesnt exist in the database or it will be rewritten.