Anirudh Lou
Anirudh Lou

Reputation: 831

How to resolve Field X doesn't have default value?

I have this code in my function that aims to update or create it's child class and this is how it looks:

$tr_routes = json_decode($request['tr_routes'], true);
if(count($tr_routes) > 0){
    foreach ($tr_routes as $tr_route) {
        if(empty($tr_route['id'])){
            $tr_route['tr_id'] = $tr_id;
            unset($tr_route['id']);
            TRequestRoutes::create($tr_route);
        }else{
            TRequestRoutes::update($tr_route);
        }
    }
}

My problem now is it gives me this error:

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `trequest_routes` (`routes_name`, `class`, `date_request`, `time`, `tr_id`, `updated_at`, `created_at`) values (asdasd, asd, asd, 2019-12-12, , 6, 2019-09-08 08:02:28, 2019-09-08 08:02:28))

I am wonder where that 'id' is coming from when in fact on the insert statement id has not been there? If i output my field $tr_route, the id is successfully unset there.

Upvotes: 1

Views: 78

Answers (1)

Ahmad Karimi
Ahmad Karimi

Reputation: 1373

It seems your ID field is not set to Autocrement. You can change your migration file and set the ID to increment there like this:

    Schema::create('tableName', function (Blueprint $table) {
        $table->increments('id');
        $table->string('yourField');
        $table->string('anotherField');
    });

and Migrate Once again.

Upvotes: 3

Related Questions