我正在学习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))
以前的例外情况
和我的clientcontroller.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>
答案 0 :(得分:0)
尝试使用$req->input('firstName')
。另外,检查HTML表单中是否存在名称为firstName,lastName甚至其余名称的输入标签。输入标签名称是必要的,因为我们在Laravel中使用 name 来获取价值。我觉得,就您而言,您可能已经错过了它,因为不仅仅是firstName,即使对于其余字段,它也没有任何价值。
所以,首先检查一下,形式:
<input type = "text" name = "firstName" placeholder = "Give name in each input tag">
如果仍然无法正常运行,请尝试
$client->firstName = $req->firstName;