Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Farirai's avatar

SQLSTATE[HY000]: General error: 1364 Field 'field' doesn't have a default value (Connection: mysql, SQL: insert into `crops`

mu CropStoreRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CropStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules(): array
    {
        return [
            'name'=>['required'],
            'field'=>['required'],
            'variety'=>['required'],
            'harvest_date'=>['required'],
            'status'=>['required'],
            'description'=>['required'],

        ];
    }
}

my migration

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('crops', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('field');
            $table->string('variety');
            $table->date('harvest_date');
            $table->string('status');
            $table->string('description');
            $table->timestamps();
        });
    }

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

my controller

<?php

namespace App\Http\Controllers;

use App\Http\Requests\CropStoreRequest;
use App\Models\Crop;
use Illuminate\Http\Request;

class CropController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('crops.index');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('crops.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(CropStoreRequest $request)
    {
        Crop::Create([
            'name' => $request->name,
            'field' => $request->field,
            'variety' => $request->variety,
            'harvest_date' => $request->harvest_date,
            'status' => $request->status,
            'description' => $request->description,
        ]);
    }

    /**
     * Display the specified resource.
     */
    public function show(Crop $crop)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Crop $crop)
    {
        return view('crops.edit', compact('crop'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Crop $crop)
    {
        $request->validate([
            'name' => 'required',
            'field' => 'required',
            'variety' => 'required',
            'harvest_date' => 'required',
            'status' => 'required',
            'description' => 'required',
        ]);
        $crop->update($request->all());
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Crop $crop)
    {
        $crop->delete();
    }
}

my create blade file

@include('layout.app')

<div class="justify-center items-center mx-auto w-5/12 mt-10">

    <h1
        class="font-bold border-2 font-sans text-transparent text-2xl bg-clip-text
        bg-gradient-to-r from-gray-700 to-blue-800">
        CREATE CROP
    </h1>

    <a href="{{ route('crops.index') }}">
        <x-page-button class="mt-5 ">
            All Crops
        </x-page-button>
    </a>

    <form enctype="multipart/form-data" method="post" action="{{ route('crops.store') }}" class="py-5">
        @csrf

        <div class="sm:col-span-6">
            <label for="name" class="block text-sm font-medium text-gray-700">Name</label>
            <div class="mt-1">
                <input type="text" id="name" name="name"
                    class="block w-full transition duration-150 ease-in-out appearance-none bg-white border-2 border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5" />
            </div>
            @error('name')
                <div class="alert alert-danger">{{ $message }}</div>
            @enderror
        </div>

        <div class="sm:col-span-6">
            <label for="field" class="block text-sm font-medium text-gray-700">Field</label>
            <div class="mt-1">
                <input type="text" id="field" name="field"
                    class="block w-full transition duration-150 ease-in-out appearance-none bg-white border-2 border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5" />
            </div>
            @error('field')
                <div class="alert alert-danger">{{ $message }}</div>
            @enderror
        </div>

        <div class="sm:col-span-6">
            <label for="variety" class="block text-sm font-medium text-gray-700">Variety</label>
            <div class="mt-1">
                <input type="text" id="variety" name="variety"
                    class="block w-full transition duration-150 ease-in-out appearance-none bg-white border-2 border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5" />
            </div>
            @error('variety')
                <div class="alert alert-danger">{{ $message }}</div>
            @enderror
        </div>

        <div class="sm:col-span-6 pt-5">
            <label for="description" class="block text-sm font-medium text-gray-700">Description</label>
            <div class="mt-1">
                <textarea id="description" rows="3" name="description"
                    class="shadow-sm focus:ring-indigo-500 appearance-none bg-white border-2 border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md"></textarea>
            </div>
            @error('description')
                <div class="alert alert-danger">{{ $message }}</div>
            @enderror
        </div>

        <div class="sm:col-span-6 pt-5">
            <label for="harvest_date" class="block text-sm font-medium text-gray-700">Harvest Date</label>
            <div class="mt-1">
                <input type="date" id="harvest_date" name="harvest_date"
                    class="block w-full transition duration-150 ease-in-out appearance-none bg-white border-2 border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5" />
            </div>
            @error('harvest_date')
                <div class="alert alert-danger">{{ $message }}</div>
            @enderror
        </div>

        <div class="sm:col-span-6">
            <label for="status" class="block text-sm font-medium text-gray-700">Status</label>
            <div class="mt-1">
                <input type="text" id="status" name="status"
                    class="block w-full transition duration-150 ease-in-out appearance-none bg-white border-2 border-gray-400 rounded-md py-2 px-3 text-base leading-normal transition duration-150 ease-in-out sm:text-sm sm:leading-5" />
            </div>
            @error('status')
                <div class="alert alert-danger">{{ $message }}</div>
            @enderror
        </div>

        <x-page-button class="mt-5">
            Create
        </x-page-button>

    </form>
</div>

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

Is the field attribute $fillable on the model?

LaryAI's avatar
Level 58

The error message "Field 'field' doesn't have a default value" indicates that the 'field' column in the 'crops' table does not have a default value set, and the INSERT statement in the 'store' method of the 'CropController' is not providing a value for this column. To fix this issue, you can either set a default value for the 'field' column in the migration file, or modify the 'store' method to provide a value for this column.

To set a default value for the 'field' column in the migration file, you can modify the 'create' method as follows:

public function up(): void
{
    Schema::create('crops', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('field')->default(''); // set default value to empty string
        $table->string('variety');
        $table->date('harvest_date');
        $table->string('status');
        $table->string('description');
        $table->timestamps();
    });
}

Alternatively, you can modify the 'store' method in the 'CropController' to provide a value for the 'field' column:

public function store(CropStoreRequest $request)
{
    Crop::Create([
        'name' => $request->name,
        'field' => $request->field ?? '', // provide default value if field is not set
        'variety' => $request->variety,
        'harvest_date' => $request->harvest_date,
        'status' => $request->status,
        'description' => $request->description,
    ]);
}

Note that the '??' operator is used to provide a default value if the 'field' parameter is not set in the request. In this case, the default value is an empty string.

Please or to participate in this conversation.