Search code examples
laravelmodelsavemass-assignment

Laravel 5.3 $model->save() values are always null however mass assignment works


Model

class Flight extends Model
{
    protected $fillable = ['name'];

    public $name;
}

In the controller

Flight::create(['name' => 'test']);
$flight = new Flight();
$flight->name = 'John';   //echo $flight->name 'John' it works
$flight->save();

The mass assignment creation works, however the method ->save() stores a null value for the object. I don't understand what I'm doing wrong. Please help!


Solution

  • Well, first of all, remove the public $name; from your model, why you need that?

    Second, of all both:

    Flight::create(['name' => 'test']);
    

    and

    $flight = new Flight();
    $flight->name = 'John';
    $flight->save();
    

    Is correct.

    And, where are you getting that null?