naekobest
4 months ago

SQLSTATE[HY000] - no default value, i dont want a default

Posted 4 months ago by naekobest

Regardless of what im trying, im getting this error message SQLSTATE[HY000]: General error: 1364 Field 'abbr' doesn't have a default value. Its not supposed to be null nor nullable. I dont quite understand why name.name does work yet abbr doesnt want to get stored.

I want to store the full name from my subject and the abbreviation.

Exam Model

class Exam extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->hasMany(Subject::class);
    }
}

Subject Model

class Subject extends Model
{
    protected $guarded = [];

    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }

    public function topics()
    {
        return $this->hasMany(Topic::class);
    }
}

Subject Controller (Store)

 public function store(Exam $exam)
    {
        $data = request()->validate([
            'name.name' => 'required',
            'abbr' => 'required'
        ]);

        $subject = $exam->subjects()->create($data['name'], ['abbr']);

        return redirect('/exam/'.$exam->id);
    }

Subject.Create

@extends('layouts.app')

@section('content')
    <div class="container">
        <form action="/exam/{{ $exam->id }}/subject" method="post">
            @csrf
            
            <div class="form-group">
                <label for="name">Name</label>
                <input name="name[name]" type="text" class="form-control" id="name" aria-describedby="nameHelp" value="{{ old('name.name') }}" autocomplete="off">
                <small id="nameHelp" class="form-text text-muted">Unterrichtsfach für <strong>Klausur: {{ $exam->name }}</strong></small>

                @error('name.name')
                <small class="text-danger">{{ $message }}</small>
                @enderror
            </div>

            <div class="form-group">
                <label for="abbr">Abkürzung</label>
                <input name="abbr" type="text" class="form-control" id="abbr" aria-describedby="abbrHelp" value="{{ old('abbr') }}" autocomplete="off">
                <small id="abbrHelp" class="form-text text-muted">Abkürzung</small>

                @error('abbr')
                <small class="text-danger">{{ $message }}</small>
                @enderror
            </div>

            <button type="submit" class="btn btn-primary">Create Subject</button>
        </form>
    </div>
@endsection

Please sign in or create an account to participate in this conversation.