mysqllaravelinsert

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value in Laravel 6


Here, I'm trying to to insert the data in the database but for some reason I am not able to insert the data in the database. This is the error:

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into stages (code, name, description, updated_at, created_at) values (32, dfs, vc, 2020-04-14 06:02:57, 2020-04-14 06:02:57))"

My code are here:

StageController.php

<?php


namespace App\Sys\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use App\Sys\Model\Stage;

class StageController extends Controller
{

    public function index(Request $request)
    {  
        $per_page = $request->per_page ? $request->per_page : 5;
        $sort_by = $request->sort_by;
        $order_by = $request->order_by;
        return response()->json(['stages' => Stage::orderBy($sort_by, $order_by)->paginate($per_page)],200);
    }

    public function store(Request $request)
    {
        $location= Stage::create([
            'code' =>$request->code,
            'name' =>$request->name,
            'description' =>$request->description
        ]);
        return response()->json(['stage'=>$stage],200);
    }


    public function show($id)                                                                                                                                                           
    {
        $stages = Stage::where('code','LIKE', "%$id%")->orWhere('name','LIKE', "%$id%")->orWhere('description', 'LIKE', "%$id%")->paginate();
        return response()->json(['stages' => $stages],200);
    }



    public function update(Request $request, $id)
    {
       $stage = Stage::find($id);
       $stage->code  = $request->code;
       $stage->name  = $request->name;
       $stage->description  = $request->description;
       $stage->save(); 
       return response()->json(['stage'=>$stage], 200);
    }

    public function destroy($id)
    {
        $stage = Stage::where('id', $id)->delete();
        return  response()->json(['stage'=>$stage],200);
    }

    public function deleteAll(Request $request){
        Stage::whereIn('id', $request->stages)->delete();
        return response()->json(['message', 'Records Deleted Successfully'], 200);
    }
}

Stage.php

<?php

namespace App\Sys\Model;

use Illuminate\Database\Eloquent\Model;

class Stage extends Model
{
    protected $guarded = [];
}

My migration file:

<?php

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

class CreateStagesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stages', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('original_id',36)->default('0')->index();
            $table->string('code',10)->index()->nullable();
            $table->string('name',100);
            $table->string('description',200)->nullable();
            $table->char('created_by',36)->index();
            $table->char('edited_by',36)->index()->nullable();
            $table->timestamps();

            $table->foreign('created_by')->references('id')->on('users');
            $table->foreign('edited_by')->references('id')->on('users');
        });
    }

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

Solution

  • For laravel 5.6+ you can use Str::uuid() to generate the uuid string;

    use Illuminate\Support\Str;
    ...
    public function store(Request $request)
        {
            $uuid = Str::uuid()->toString();
            $location= Stage::create([
                'id' => $uuid,
                'code' =>$request->code,
                'name' =>$request->name,
                'description' =>$request->description
            ]);
            return response()->json(['stage'=>$stage],200);
        }
    

    For below laravel 5.6, you can use Ramsey\Uuid\Uuid;

    use Ramsey\Uuid\Uuid;
    ...
    public function store(Request $request)
        {
            $uuid = Uuid::uuid1()->toString();
            ...
            return response()->json(['stage'=>$stage],200);
        }
    

    Or you can write an boot method for generating uuid to creating, Eloquent will automatically set id=uuid for every create method. If there are many models with primary key uuid, you can write a trait and use this trait in each models.

    use Illuminate\Support\Str;
    ...
    class Stage extends Model
    {
    
        /**
         * Boot function from laravel.
         */
        protected static function boot()
        {
            parent::boot();
    
            static::creating(function ($model) {
                $model->{$model->getKeyName()} = Str::uuid()->toString();
            });
        }
    }