【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstName' cannot be null (23000)SQLSTATE [23000]:违反完整性约束:1048 列 'firstName' 不能为空 (23000)
【发布时间】: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="&bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;">
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary" type="submit">Submit</button>
                    </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 尝试将 $req-&gt;firstName; 更改为 $req-&gt;input('firstName'); 并对所有其他输入执行相同操作。
  • 您可以使用dd() 等调试功能来检查您在发送请求时是否正确设置了$req-&gt;firstName$client-&gt;firstName
  • 是否有任何其他更新方式可以让我执行相同的操作或更安全或仅此而已?
  • @ElektaKode 你能举个例子吗?
  • @RafMavrogordatos : 在store 方法的第一行尝试dd($req-&gt;firstName) 并查看结果。

标签: php mysql laravel


【解决方案1】:

尝试使用$req-&gt;input('firstName')。此外,检查 HTML 表单中是否存在名称为 firstName、lastName 甚至其余名称的输入标记。为输入标签命名是必要的,因为我们在 Laravel 中使用 name 取值。我觉得,在您的情况下,您可能错过了它,因为不仅仅是名字,即使对于其他字段,它也没有任何价值。

所以,首先检查,在表单中:

<input type = "text" name = "firstName" placeholder = "Give name in each input tag">

如果还是不行,试试

$client->firstName = $req->firstName;

【讨论】:

    猜你喜欢
    • 2019-11-20
    • 2021-08-16
    • 2019-10-06
    • 2020-10-25
    • 2017-02-28
    • 2017-04-15
    • 2017-12-26
    • 2019-11-24
    • 2018-01-25
    相关资源
    最近更新 更多