I am getting error in Laravel 5.7:
IlluminateDatabaseQueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘name’ cannot be null (SQL: insert into
stores
(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:
JavaScript
x
34
34
1
<!DOCTYPE html>
2
<html>
3
<head>
4
@section('title', 'Sign up for customer page')
5
</head>
6
<body class="text-center">
7
@extends('layout.app')
8
9
@section('content')
10
11
@endsection
12
@include('include.navbar')
13
<form class="form-signup" action="{{URL:: to('/store')}}" method="post">
14
@csrf
15
16
<h1 class="h3 mb-3 font-weight-normal">Please sign up as customer</h1>
17
<label for="inputname" class="sr-only">Name</label>
18
<input type="text" id="inputname" class="form-control" placeholder="Name" required>
19
<label for="inputmatric" class="sr-only">ID matric</label>
20
<input type="text" id="inputmatric" class="form-control" placeholder="ID matric" required>
21
<label for="inputphon" class="sr-only">Phone No</label>
22
<input type="text" id="inputphon" class="form-control" placeholder="Phone No" required>
23
<label for="inputemail" class="sr-only">E-mail</label>
24
<input type="text" id="inputemail" class="form-control" placeholder="E-mail" required>
25
<label for="inputpassword" class="sr-only">Password</label>
26
<input type="text" id="inputpassword" class="form-control" placeholder="Password" required>
27
28
29
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign up</button>
30
<p class="mt-5 mb-3 text-muted">© 2017-2019</p>
31
</form>
32
33
</html>
34
This is UserController
:
JavaScript
1
37
37
1
namespace AppHttpControllers;
2
use AppHttpControllersController;
3
use IlluminateHttpRequest;
4
use IlluminateSupportFacadesURL;
5
use IlluminateSupportFacadesDB;
6
use Appstore;//model name
7
8
class UserController extends Controller
9
{
10
public function store(Request $request)
11
{
12
13
$this->validate($request,[
14
'name'=> 'request',
15
'matric'=> 'request',
16
'phone'=> 'request',
17
'email'=>'request',
18
'password'=> 'request'
19
20
]);
21
22
//store new customer
23
$store = new store; // valible and model name
24
$store-> name = $request->input('name');
25
$store-> matric = $request->input('matric');
26
$store-> phone = $request->input('phone');
27
$store-> email = $request->input('email');
28
$store-> password = $request->input('password');
29
30
//save new customer
31
$store-> save();
32
33
//redirect
34
return redirect('/');
35
}
36
}
37
This is the Migration:
JavaScript
1
37
37
1
<?php
2
//create the customer table
3
use IlluminateSupportFacadesSchema;
4
use IlluminateDatabaseSchemaBlueprint;
5
use IlluminateDatabaseMigrationsMigration;
6
7
class CreateStoresTable extends Migration
8
{
9
/**
10
* Run the migrations.
11
*
12
* @return void
13
*/
14
public function up()
15
{
16
Schema::create('stores', function (Blueprint $table) {
17
$table->bigIncrements('id');
18
$table->string ('name');
19
$table->integer ('matric');
20
$table->string ('phone');
21
$table->string ('email');
22
$table->string ('password');
23
$table->timestamps();
24
});
25
}
26
27
/**
28
* Reverse the migrations.
29
*
30
* @return void
31
*/
32
public function down()
33
{
34
Schema::dropIfExists('stores');
35
}
36
}
37
Advertisement
Answer
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 the name
attribute to the input fields.
JavaScript
1
11
11
1
<label for="inputname" class="sr-only">Name</label>
2
<input type="text" id="inputname" name="name" class="form-control" placeholder="Name" required>
3
<label for="inputmatric" class="sr-only">ID matric</label>
4
<input type="text" id="inputmatric" name="matric" class="form-control" placeholder="ID matric" required>
5
<label for="inputphon" class="sr-only">Phone No</label>
6
<input type="text" id="inputphon" name="phone" class="form-control" placeholder="Phone No" required>
7
<label for="inputemail" class="sr-only">E-mail</label>
8
<input type="text" id="inputemail" name="email" class="form-control" placeholder="E-mail" required>
9
<label for="inputpassword" class="sr-only">Password</label>
10
<input type="text" id="inputpassword" name="password" class="form-control" placeholder="Password" required>
11
And also change your validation
JavaScript
1
8
1
$this->validate($request,[
2
'name'=> 'required',
3
'matric'=> 'required',
4
'phone'=> 'required',
5
'email'=>'required',
6
'password'=> 'required'
7
]);
8