SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `cats` (`updated_at`, `created_at`)

Viewed 19659

when I'm trying to store a new row this error appears

SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value

(SQL: insert into cats (updated_at, created_at) values (2020-08-08 22:32:29, 2020-08-08 22:32:29))

using Laravel 5.8

my Controller

public function store(Request $request)
{
    $form_data = array(
        'name'  =>  $request->name,
    );
    
    Cat::create($form_data);

    return redirect('admin')->with('success', 'Data Added successfully.');
    //Cat::create($form_data);
    dd($request->name);
}

my model is:

<?PHP
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;

class Cat extends Model
{
    use Notifiable;

    protected $guarded = [];
    protected $fillable  = ['nama'];
    
    public function products()
    {
        return $this->hasMany(Product::class);
    }
}
2 Answers

You have a typo mistake in your model for the fillable variable ('name' not 'nama')

protected $fillable = ['nama'];

Correct it with:

protected $fillable  = ['name'];

You have a Typo in your model Change nama to name

Always be mindful of typos in your code as abeginner

Related