Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

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()".

Last updated 2 years ago.
0

Shouldn't it be

parent::__construct($attributes);

?

0

Thanks! It works! (it looks so obvious now, that I think I must visit an ophthalmologist...)

0

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?

Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

panosss panosss Joined 18 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.