I am getting error in Laravel 5.7:
IlluminateDatabaseQueryException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘name’ cannot be null (SQL: insert intostores
(name
,matric
,phone
,password
,updated_at
,created_at
) values (?, ?, ?, ?, ?, 2019-10-01 16:29:49, 2019-10-01 16:29:49))
This is my form:
<!DOCTYPE html>
<html>
<head>
@section('title', 'Sign up for customer page')
</head>
<body class="text-center">
@extends('layout.app')
@section('content')
@endsection
@include('include.navbar')
<form class="form-signup" action="{{URL:: to('/store')}}" method="post">
@csrf
<h1 class="h3 mb-3 font-weight-normal">Please sign up as customer</h1>
<label for="inputname" class="sr-only">Name</label>
<input type="text" id="inputname" class="form-control" placeholder="Name" required>
<label for="inputmatric" class="sr-only">ID matric</label>
<input type="text" id="inputmatric" class="form-control" placeholder="ID matric" required>
<label for="inputphon" class="sr-only">Phone No</label>
<input type="text" id="inputphon" class="form-control" placeholder="Phone No" required>
<label for="inputemail" class="sr-only">E-mail</label>
<input type="text" id="inputemail" class="form-control" placeholder="E-mail" required>
<label for="inputpassword" class="sr-only">Password</label>
<input type="text" id="inputpassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
<p class="mt-5 mb-3 text-muted">© 2017-2019</p>
</form>
</html>
This is UserController
:
namespace AppHttpControllers;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesURL;
use IlluminateSupportFacadesDB;
use Appstore;//model name
class UserController extends Controller
{
public function store(Request $request)
{
$this->validate($request,[
'name'=> 'request',
'matric'=> 'request',
'phone'=> 'request',
'email'=>'request',
'password'=> 'request'
]);
//store new customer
$store = new store; // valible and model name
$store-> name = $request->input('name');
$store-> matric = $request->input('matric');
$store-> phone = $request->input('phone');
$store-> email = $request->input('email');
$store-> password = $request->input('password');
//save new customer
$store-> save();
//redirect
return redirect('/');
}
}
This is the Migration:
<?php
//create the customer table
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateStoresTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stores', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string ('name');
$table->integer ('matric');
$table->string ('phone');
$table->string ('email');
$table->string ('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stores');
}
}
5
Answers
You are trying to save a null value in the name column even though it is required.
You are missing
name
attribute in your html form. Without this attribute your input data won’t be passed to the controller and thus you are getting empty values. So add thename
attribute to the input fields.And also change your validation
You should name your input with
name="namehere"
e.g
You can also take below steps to bypass this error in your Laravel website :-
Update
'strict' => false
indatabase.php
file located at pathconfig
Comment below statement in
Kernel.php
file located at pathappHttp
Hope it will help someone in future.
You might be sending data in text format:
. text pic
. json pic