~/vladvisinescu_
<- cd ~/tutorials

tutorial

Quickly debug a Laravel fillable error

When Laravel says a field such as name is not in the model's fillable array, it has blocked a mass-assignment operation. The fix is usually small, but first confirm which model and payload are involved.

1. Find the mass-assignment call

Look for create(), update(), fill(), or forceFill() near the top of the stack trace. A typical controller might contain:

$validated = $request->validate([
    'name' => ['required', 'string', 'max:255'],
    'email' => ['required', 'email'],
]);

User::create($validated);

Temporarily inspect the exact data being passed to the model:

dd($validated);

If name is present, check that User::create() is really using the model you expect. A wrong import is easy to miss:

use App\Models\User;

2. Check the model

Open the model from the exception or stack trace. Every field passed through mass assignment must be allowed explicitly:

class User extends Authenticatable
{
    protected $fillable = [
        'name',
        'email',
        'password',
    ];
}

Watch for small mismatches: full_name in the request is not the same field as name in $fillable. Also confirm the column exists in the migration.

$table->string('name');

3. Keep the payload narrow

Do not fix the exception by blindly allowing every request field. Validate the input, then pass only the attributes that the operation needs:

$user = User::create([
    'name' => $validated['name'],
    'email' => $validated['email'],
    'password' => Hash::make($validated['password']),
]);

Using protected $guarded = []; disables mass-assignment protection for the model. That can be intentional in tightly controlled code, but it is usually too broad for request-driven data.

4. Add a quick regression test

A feature test confirms both the fix and the expected database value:

test('a user can be created with a name', function () {
    $response = $this->post('/users', [
        'name' => 'Ada Lovelace',
        'email' => 'ada@example.com',
        'password' => 'secret-password',
    ]);

    $response->assertRedirect();

    $this->assertDatabaseHas('users', [
        'name' => 'Ada Lovelace',
        'email' => 'ada@example.com',
    ]);
});

Run the smallest relevant test first:

php artisan test --filter="a user can be created with a name"

The fast debugging path is: read the stack trace, inspect the payload, verify the model import, compare the payload keys with $fillable, and lock the fix in with a test.

  1. 01 Code block tabs: one task, three languages
  2. 02 primul tutorial