DucCuong
DucCuong

Reputation: 648

Laravel Eloquent Model inserts all values as NULL

I have run into problems with Laravel Eloquent Model

I have a model as follow:

class Activity extends Eloquent {
    protected $table = 'activity';

    protected $timestamps = false;

    public $item;

    public $content;

    public $year;

    protected $fillable = array('item', 'content', 'year');
}

And the corresponding controller:

class ActivityController extends \BaseController {
     public function create()
     {
         $activity = new Activity();

         $actitity->item = 'Example';
         $activity->content = 'Example content';
         $activity->year = 2015;

         $activity->save();
     }
}

The above code should work fine and there should be a record in 'activity' table. However, all the value of columns of activity table are inserted as NULL when I run this code (except for the id column which is auto_increment).

In addition, when I var_dump the $activity (just before calling $activity->save()), the $activity with all of its properties are shown as expected (I mean, with values I've assigned before)

Is there any subtle error in my code?

Upvotes: 2

Views: 2841

Answers (3)

henrik
henrik

Reputation: 1618

Remove:

public $item;

public $content;

public $year;

from:

class Activity extends Eloquent {
    protected $table = 'activity';

    protected $timestamps = false;

    public $item;

    public $content;

    public $year;

    protected $fillable = array('item', 'content', 'year');
}

Upvotes: 0

lukasgeiter
lukasgeiter

Reputation: 152860

You must not define database fields as actual class properties. The problem is that Laravel uses an $attributes array internally, not the models properties.

When doing

$activity->content = 'Example content';

Laravel uses the magic __set() method to update the value in it's $attributes array. But that setter method is never called because you have an actual property with that name.

What you need to do to resolve this problem is remove the properties:

class Activity extends Eloquent {
    protected $table = 'activity';

    protected $timestamps = false;

    protected $fillable = array('item', 'content', 'year');
}

If you want to document the properties and have autocomplete support you can use the @property annotation:

/**
 * @property string $item
 * @property string $content
 * @property int $year
 */
class Activity extends Eloquent {

Upvotes: 2

SamV
SamV

Reputation: 7586

This is because Eloquent uses magic setters/getters. If you did $model->randomAttribute then it would look into the models attributes array for the data.

Because you have explicitly defined each attribute it directly accesses the property and not the magic getter. When you call save(), the function saves all the data in the attributes array which contains nothing.

Remove the attribute definitions and it will work.

If you call $model->getAttributes() you will see there will be no data contained within.

Upvotes: 0

Related Questions