Add [title] to fillable property to allow mass assignment on [App\Post].

Published 1 month ago by hamilton

Hi Folks,

I'm new to Laravel (5.7) and trying to save to DB using Post::create()

Despite having:

protected $fillable = ['title','content'];

in the Post model when I attempt to run:

Post::create([
'title' => 'Post Title',
    'content' => 'Post Content' 
]);

I get:

"Add [title] to fillable property to allow mass assignment on [App\Post]."

I searched everywhere for solution but come up with nothing so for.

Any help appreciated. If more info required let me know.

Thanks

oneshoe

Shouldn't $fillable be a public property?

Edit: sorry, protected is correct.

hamilton

I created the model in Terminal using:

php artisan make:model Post

Resulting code generated:

use SoftDeletes;

protected $dates = ['deleted_at'];
 
protected $fillable = ['title','content'];
    
public function user() {
    return $this->belongsTo('App\User');
}
 
Snapey
Snapey
1 month ago (1,036,605 XP)

open tinker and type dump(new Post)

Is $fillable listed?

hamilton

Hi guys,

Thanks for your suggestions.

Turns out I working on a model of the same name but in a different but similarly named project!

I added:

protected $fillable = ['title','content'];

Then everything worked as expected.

Sorry for wasting your time.

Please sign in or create an account to participate in this conversation.