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 AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Student extends Model
{
use HasFactory;
protected $fillable = ['name','nickname','email','phone','password'];
}
This is my Controller file:
use IlluminateHttpRequest;
use AppModelsStudent;
class StudentController extends Controller
{
/**
* Display a listing of the resource.
*
// * @return IlluminateHttpResponse
*/
public function index()
{
$student = Student::all();
return view('index', compact('student'));
}
/**
* Show the form for creating a new resource.
*
// * @return IlluminateHttpResponse
*/
public function create()
{
return view('create');
}
/**
* Store a newly created resource in storage.
*
// * @param IlluminateHttpRequest $request
// * @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
// * @param int $id
// * @return IlluminateHttpResponse
*/
public function edit($id)
{
$student = Student::findOrFail($id);
return view('edit', compact('student'));
}
/**
* Update the specified resource in storage.
*
// * @param IlluminateHttpRequest $request
// * @param int $id
// * @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
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.
4
Answers
Actually I made a typo:
False:
True:
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.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.
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)?