Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

minasrc's avatar

Mass assignment not working with $fillable nor $guarded

I am trying to mass assign instances of a Category Model, however, I keep getting the MassAssignment Exception . I already added the $fillable property on my columns then changed it to $guarded=[]; in the Model's class and none the approaches seem to work ( restarted the psy shell session as well) any ideas where the issue might be?

0 likes
16 replies
Sinnbeck's avatar

Show the model and how you are using create()

tykus's avatar

Can you share the relevant code?

minasrc's avatar

This is the model:

class Category extends Model
{
    
    use HasFactory;
    protected $guarded = ['id'];
}

And here is how I call create in the psy shell:

Category::create(['name'=>'Git', 'slug'=>'git']);  

The error specifically mentions the name column ( obviously the first it encounters) any chance am missing an import?? These are the ones I have so far in my Model:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
minasrc's avatar

@Sinnbeck

Illuminate\Database\Eloquent\MassAssignmentException with message 'Add [name] to fillable property to allow mass assignment on [App\Models\Category].

I was wondering if am using the wrong Eloquent class or missing an import cause it seems to be defaulting to something different from what am defining, I refreshed and even did the old restart the server trick, but nothing is changing :(

minasrc's avatar

@Sinnbeck The Category Model:


namespace App\Models;

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

class Category extends Model
{
    
    use HasFactory;
    protected $guarded = ['id'];
}

and the migrations class:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string("name");
            $table->text("slug")->unique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}
minasrc's avatar

Should I be recreating the model if I make change or something? cause I looked if there's a sort of a refresh for the models but nothing came up from the man of the command.

Sinnbeck's avatar

@minasrc no you should just be able to change it. Only places where it might not update would be php artisan tinker (and queues)

1 like
Sinnbeck's avatar

Missed that you wrote psy shell, so tykus is correct. Tinker might not be able to detect changes and you need to restart it

You threw me off when you said you restarted "the server"

minasrc's avatar

@Sinnbeck yes I was restarting the session everytime then ended up restarting the server cause I couldn't see where the problem was. Eventually turns out VSCode was crashing and somehow decided to automatically disable my auto save -_-#

tykus's avatar

This is in a Tinker session, right? Did you restart Tinker whenever you changed the Model (or other classes)?

1 like
minasrc's avatar

@tykus yes. I restarted the session, then even restarted the local server. I know for sure it's not keeping record in the session cause it keeps aliasing everytime I can create (or at least I think that's proof enough)

minasrc's avatar

@tykus ofc I am xD solved by VSCode which was also the cause of the issue. It seems VSCode gave up on me for a while and was not updating the files ( I have auto save on) and not notifying me to save even when I close them! That's embarrassing to say the least but thank you all for the support.

Please or to participate in this conversation.