Show the model and how you are using create()
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?
Can you share the relevant code?
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 can you give the exact error?
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 everything looks correct. Can you share the complete Category class?
@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');
}
}
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.
@minasrc no you should just be able to change it. Only places where it might not update would be php artisan tinker (and queues)
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"
@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 -_-#
This is in a Tinker session, right? Did you restart Tinker whenever you changed the Model (or other classes)?
@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 you are in the correct project directory, right?
@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.
@minasrc 🤦♂️
Please or to participate in this conversation.