Search code examples
phplaravellaravel-4eloquentseeding

Constructors on classes extending Eloquent


I just started a new website and I wanted to make use of Eloquent. In the process of seeding my database, I noticed that I would get empty rows added if I had included any kind of constructor on the model that extends eloquent. For example, running this seeder:

<?php

class TeamTableSeeder extends Seeder {

    public function run()
    {
        DB::table('tm_team')->delete();

        Team::create(array(
            'city' => 'Minneapolis',
            'state' => 'MN',
            'country' => 'USA',
            'name' => 'Twins'
            )
        );

        Team::create(array(
            'city' => 'Detroit',
            'state' => 'MI',
            'country' => 'USA',
            'name' => 'Tigers'
            )
        );
    }

}

With this as my Team class:

<?php

class Team extends Eloquent {

    protected $table = 'tm_team';
    protected $primaryKey = 'team_id';

    public function Team(){
        // null
    }
}

Yields this:

team_id | city  | state | country   | name  | created_at            | updated_at            | deleted_at
1       |       |       |           |       | 2013-06-02 00:29:31   | 2013-06-02 00:29:31   | NULL
2       |       |       |           |       | 2013-06-02 00:29:31   | 2013-06-02 00:29:31   | NULL

Simply removing the constructor all together allows the seeder to work as expected. What exactly am I doing wrong with the constructor?


Solution

  • You have to call parent::__construct to make things work here, if you look at the constructor of the Eloquent class:

    public function __construct(array $attributes = array())
    {
        if ( ! isset(static::$booted[get_class($this)]))
        {
            static::boot();
    
            static::$booted[get_class($this)] = true;
        }
    
        $this->fill($attributes);
    }
    

    The boot method is called and the booted property is set. I don't really know what this is doing but depending on your problem it seems relevant :P

    Refactor your constructor to get the attributes array and put it to the parent constructor.

    Update

    Here is the needed code:

    class MyModel extends Eloquent {
        public function __construct($attributes = array())  {
            parent::__construct($attributes); // Eloquent
            // Your construct code.
        }
    }