【发布时间】:2021-02-01 19:16:50
【问题描述】:
我正在学习 laravel,此时我正在尝试将查询从我使用 CoreUI 作为管理模板构建的基础发送回我的数据库。
所以基本上我得到了这个错误:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null (23000)
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null (SQL: insert into `clients` (`firstName`, `lastName`, `companyName`, `phone`, `email`, `password`, `address1`, `address2`, `city`, `postcode`, `country`, `notes`, `updated_at`, `created_at`) values (, , , , , , , , , , , , 2020-10-19 01:41:04, 2020-10-19 01:41:04))
以前的例外情况
我的 clientscontroller.php 包含此代码
use App\Client;
public function store(Request $req){
$client = new Client;
$client->firstName = $req->firstName;
$client->lastName = $req->lastName;
$client->companyName = $req->companyName;
$client->phone = $req->phone;
$client->email = $req->email;
$client->password = $req->password;
$client->address1 = $req->address1;
$client->address2 = $req->address2;
$client->city = $req->city;
$client->postcode = $req->postcode;
$client->country = $req->country;
$client->notes = $req->notes;
$client->save();
return redirect('addclient')->with('status','Client added successfully!');
}
在我的模型 client.php 中我没有包含任何内容,因为我看过 20 个教程,但我找不到我应该做什么,所以我只包含了那些 2
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
我认为正确的路线 所以有谁知道我错过了什么,因为在网络上没有明确的方式将数据从刀片发送到 mysql。
非常感谢!
编辑:
这是表格:
<h4 class="card-title">Add new Client</h4>
<p class="card-description">Enter customer information</p>
<form action="{{route('save')}}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" class="form-control" id="firstName" placeholder="John">
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" class="form-control" id="lastName" placeholder="Doe">
</div>
<div class="form-group">
<label for="companyName">Company Name</label>
<input type="text" class="form-control" id="companyName" placeholder="WEBSUNRISE">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" placeholder="+44.7545958574">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" placeholder="name@example.com">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="••••••••">
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</div>
</div>
【问题讨论】:
-
尝试将
$req->firstName;更改为$req->input('firstName');并对所有其他输入执行相同操作。 -
您可以使用
dd()等调试功能来检查您在发送请求时是否正确设置了$req->firstName或$client->firstName。 -
是否有任何其他更新方式可以让我执行相同的操作或更安全或仅此而已?
-
@ElektaKode 你能举个例子吗?
-
@RafMavrogordatos : 在
store方法的第一行尝试dd($req->firstName)并查看结果。