Вопрос

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?

Это было полезно?

Решение

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.
    }
}

Другие советы

In laravel 3 you must put the second parameter '$exists' with default value "false".

class Model extends Eloquent {

    public function __construct($attr = array(), $exists = false) {
        parent::__construct($attr, $exists);
       //other sentences...
    }
}

You can use this generic method that allows you to pass a parameter too.

/**
* Overload model constructor.
*
* $value sets a Team's value (Optional)
*/
public function __construct($value = null, array $attributes = array())
{
     parent::__construct($attributes);
     $this->value = $value;
     // Do other staff...    
}

I faced the same problem and I solved passing arguments with the constructor.

class MyModel extends Model
{
    public function __construct(array $attributes = []) {
        parent::__construct($attributes);

        // ...
    }
 }

Then call like regular class,

$result = (new MyModel($request->all()))->create($request->all());
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top