Example taken form http://stackoverflow.com/questions/26757452/laravel-eloquent-accessing-properties-and-dynamic-table-names
class Team extends \Illuminate\Database\Eloquent\Model {
public function __construct($attributes = [], $year = null) {
parent::construct($attributes);
$year = $year ?: date('Y');
$this->setTable("gamedata_$year_teams");
}
// Your other stuff here...
}
I call it:
$myTeam = new Team([], 2015);
And I get the error: "Call to undefined method Illuminate\Database\Query\Builder::construct()".
Thanks! It works! (it looks so obvious now, that I think I must visit an ophthalmologist...)
And how do I get a record? These don't work:
$t=$myTeam ->find($id);
$t=$myTeam ::find($id);
Maybe I'm doing it the wrong way.
What I want to do is change table of a Model, dynamically.
Is this the correct way?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community