66

While inserting data in Mysql I have encountered the following error:

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

Here is my code:

$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);

While when I use another way to insert data, it is working fine: Following code is working fine :

//Create Post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
   

Could anyone explain why an upper portion of the code is throwing an error?

Sarthak Raval
  • 1,001
  • 1
  • 10
  • 23
Muhammad Mansha
  • 827
  • 1
  • 7
  • 12
  • Possible duplicate of [What does "Mass Assignment" mean in Laravel?](https://stackoverflow.com/questions/22279435/what-does-mass-assignment-mean-in-laravel) – Walter Cejas Dec 15 '18 at 14:14

12 Answers12

113

Add a title to the fillable array in your model Post, to allow saving through creating and massive methods

protected $fillable = ['title'];
Sarthak Raval
  • 1,001
  • 1
  • 10
  • 23
Walter Cejas
  • 1,924
  • 1
  • 12
  • 22
61

The alternative to protected $fillable = ['title']; would be :

protected $guarded = [];  

and leave it as an empty array, without the need to define anything inside. It is the exact opposite of $fillable, sort of like telling the database to accept everything, except the fields you specify inside the $guarded array.

Armin
  • 2,021
  • 2
  • 19
  • 17
16

For $fillable all

protected $guarded = ['id']; 
Alimon Karim
  • 4,354
  • 10
  • 43
  • 68
10

For mass assignment you should define "Fillable array" in your model (App\Post)

So your model should be something like this:

    class Post extends Model
    {

        protected $fillable = ['title','body']; //<---- Add this line
// ... 
}

More information: [https://laravel.com/docs/5.7/eloquent#mass-assignment][1]

Saman Ahmadi
  • 677
  • 3
  • 9
6

Add a title to the fillable array in your model Post.

protected $fillable = ['title'];
Omar Badr
  • 61
  • 1
  • 5
  • 2
    Hi, Welcome to Stack Overflow. As explained in the [tour](https://stackoverflow.com/tour), this site is a repository of useful questions and their answers. Your answer is (included in)/(very similar to) [this answer](https://stackoverflow.com/a/53793892/8068675), and not very useful since it does not add any new value or information. Please avoid writing duplicate answers. Either edit your answer to add value or delete it altogether; this will ensure all questions and answers on the site remain useful and not scattered/duplicated. – Clément Baconnier Oct 19 '21 at 09:50
3

This error appeared because you didn't declare a variable in your Model - you should define it in your class like so;

protected $fillable = ['title'];
Rob
  • 4,927
  • 4
  • 26
  • 41
Punit khandelwal
  • 129
  • 1
  • 13
1

All Laravel Models when created, don't contain any fillable property.

To allow them to accept field data and insert to database, we must first allow them in our model.

In your Post model, add this line:

protected $fillable = ['title'];
Prem Sagar
  • 147
  • 2
  • 8
1

I'm late to the party, but I had this problem and the solution was completely different.

Inside my PostController I was checking auth, then finding:

Post::where('slug', $oldSlug)->first()->update([
            'image_url' => $request->image_url,
            'slug' => $newSlug,
            'title' => $request->title,
            'body' => $request->body,
            'description' => $request->description,
            'user_id' => auth()->user()->id,
            'catagory' => $request->catagory,
        ]);

I was looking at it trying to understand why it didn't like "image_url", screaming "Add ['image_url'] to fillable property to allow mass assignment".

I realized that I added the ->first() to make sure I was only getting one post, but since all slugs are validated and verified to be unique, I dropped the ->first().

Post::where('slug', $oldSlug)->update([
        'image_url' => $request->image_url,
        'slug' => $newSlug,
        'title' => $request->title,
        'body' => $request->body,
        'description' => $request->description,
        'user_id' => auth()->user()->id,
        'catagory' => $request->catagory,
    ]);

Like magic, it works.

If anyone with more advanced knowledge can explain why it worked, I would be very interested in knowing. Thanks!

0

In your Post model, there will be a $fillable array variable which you have to add 'title'.

For More info: What does "Mass Assignment" mean in Laravel?

Vinesh Goyal
  • 607
  • 5
  • 8
0

the best option to prevent one field

Post::create($request->except('_token'));
azhar
  • 351
  • 3
  • 13
-1

Check the docs here

Alternatively, you may use the create method to "save" a new model using a single PHP statement. The inserted model instance will be returned to you by the create method:

use App\Models\Flight;

$flight = Flight::create([
    'name' => 'London to Paris',
]);

However, before using the create method, you will need to specify either a fillable or guarded property on your model class. These properties are required because all Eloquent models are protected against mass assignment vulnerabilities by default.

Adelin
  • 18,144
  • 26
  • 115
  • 175
-1

Laravel 9 - Add [title] to fillable property to allow mass assignment on [App\Post] - ERROR

For mass assignment defined "Fillable array" on the model (App\Post)

So post model should be like this:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;
    protected $fillable = ['title','body','excerpt','is_published','image_path','is_published','min_to_read'];
}

This worked for me!

Ashok
  • 800
  • 11
  • 20