Home > Enterprise >  SQLSTATE[HY000]: General error: 1364 Field 'nickname' doesn't have a default value
SQLSTATE[HY000]: General error: 1364 Field 'nickname' doesn't have a default value

Time:11-22

I'm doing CRUD operations with Laravel and I added a new input to my 'create' form, "nickname" but I'm getting this error: SQLSTATE[HY000]: General error: 1364 Field 'nickname' doesn't have a default value

INSERT INTO
  `students` (
    `name`,
    `email`,
    `phone`,
    `password`,
    `updated_at`,
    `created_at`
  )
VALUES
  (
    test,
    [email protected],
    99999999,
    testpassword,
    2022 -11 -21 13: 20: 47,
    2022 -11 -21 13: 20: 47
  )

This is my Model file:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;
    protected $fillable = ['name','nickname','email','phone','password'];
}

This is my Controller file:

use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
//     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $student = Student::all();
        return view('index', compact('student'));
    }

    /**
     * Show the form for creating a new resource.
     *
//     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('create');
    }


    /**
     * Store a newly created resource in storage.
     *
//     * @param  \Illuminate\Http\Request  $request
//     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $storeData = $request->validate([
            'name' => 'required|max:255',
            'name' => 'required|max:255',
            'email' => 'required|max:255',
            'phone' => 'required|numeric',
            'password' => 'required|max:255',
        ]);
        $student = Student::create($storeData);
        return redirect('/students')->with('completed', 'Student has been saved!');
    }

    /**
     * Display the specified resource.
     *
//     * @param  int  $id
//     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
//     * @param  int  $id
//     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $student = Student::findOrFail($id);
        return view('edit', compact('student'));
    }

    /**
     * Update the specified resource in storage.
     *
//     * @param  \Illuminate\Http\Request  $request
//     * @param  int  $id
//     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $updateData = $request->validate([
            'name' => 'required|max:255',
            'nickname' => 'required|max:255',
            'email' => 'required|max:255',
            'phone' => 'required|numeric',
            'password' => 'required|max:255',
        ]);
        Student::whereId($id)->update($updateData);
        return redirect('/students')->with('completed', 'Student has been updated');
    }

    /**
     * Remove the specified resource from storage.
     *
//     * @param  int  $id
//     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $student = Student::findOrFail($id);
        $student->delete();
        return redirect('/students')->with('completed', 'Student has been deleted');
    }
}

When I remove the required option in nickname it works but I have to do this.

CodePudding user response:

Solve 1 ::

the nickname column seems not setted to nullable column. Alter nickname column to nullable in table.

Solve 2 ::

or simply you can set it's default value on nickname column.

Solve 3 ::

you can add nickname column in your create SQL Query.

INSERT INTO
  `students` (
    `name`,
    `nickname`,
    `email`,
    `phone`,
    `password`,
    `updated_at`,
    `created_at`
  )
VALUES
  (
    test,
    nickname
    [email protected],
    99999999,
    testpassword,
    2022 -11 -21 13: 20: 47,
    2022 -11 -21 13: 20: 47
  )

CodePudding user response:

Adding to xlab's solutions, you could also disable STRICT_TRANS_TABLES. This is particularly useful when you already have the code written and you can "allow" not to use strict inputs (if you understand the implications).

Check the accepted solution here:

How to turn on/off MySQL strict mode in localhost (xampp)?

CodePudding user response:

It seems that You have not set default value of nickname in database as Null. If you are not passing any value then it must require to set default value in either code of in database.

CodePudding user response:

Actually I made a typo:

False:

 public function store(Request $request)
{
    $storeData = $request->validate([
        'name' => 'required|max:255',
        'name' => 'required|max:255',
        'email' => 'required|max:255',
        'phone' => 'required|numeric',
        'password' => 'required|max:255',
    ]);

True:

 public function store(Request $request)
{
    $storeData = $request->validate([
        'name' => 'required|max:255',
        '**nickname**' => 'required|max:255',
        'email' => 'required|max:255',
        'phone' => 'required|numeric',
        'password' => 'required|max:255',
    ]);
  • Related