sql完整性约束错误

nhjlsmyf  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(339)

目前正在使用Laravel5.6,更新我创建的表单时遇到问题。
完全错误是:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: update `companies` set `name` = , `description` = this is a SE company., `updated_at` = 2018-12-16 10:05:55 where `id` = 1)

edit.blade.php文件如下:

<form method="post" action="{{route('companies.update',[$company->id]) }}">
        {{ csrf_field() }}

        <input type="hidden" name="_method" value="put">

        <div class="form-group">
            <label for="company-name">Name<span class="required">*</span></label>
            <input placeholder="Enter name"
                   id="company-name"
                   required
                   name="description"
                   spellcheck="false"
                   class="form-control"
                   value="{{ $company->name }}"
            />
        </div>

        <div class="form-group">
            <label for="company-content">Description</label>
            <textarea placeholder="Enter description"
                        style="resize:vertical"
                        name="description" 
                        id="company-content" 
                        rows="5" cols="5"
                        spellcheck="false"
                        class="form-control autosize-target text-left">
                        {{ $company->description}}
                    </textarea>
        </div>
        <div class="form-group">
            <input type="submit" class="btn btn-primary"
                    value="Submit"/>
        </div>

      </form>

这是迁移文件:

public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->longText('description')->nullable();
        $table->integer('user_id')->unsigned();
        $table->timestamps();
    });
}

任何关于我做错了什么的指导都将不胜感激。

nfg76nw0

nfg76nw01#

输入属性名称不正确

<input placeholder="Enter name"
               id="company-name"
               required
               name="description" <!-- <<-- This should be "name" not "description" -->
               spellcheck="false"
               class="form-control"
               value="{{ $company->name }}"
/>

如果出于某种原因你想让name可以为null

public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        // ...   
        $table->string('name')->nullable(); // <<-- this is modified
        // ...
    });
}
ttisahbt

ttisahbt2#

(请注意,一旦包含了php代码片段,我将立即更新我的答案,因为这可能是导致错误的部分原因。)
您在输入元素中使用了错误的name属性。你检查了两次 description 现在。
更改此项:

<input placeholder="Enter name"
                   id="company-name"
                   required
                   name="description"
                   spellcheck="false"
                   class="form-control"
                   value="{{ $company->name }}"
            />

对此:

<input placeholder="Enter name"
                   id="company-name"
                   required
                   name="name"
                   spellcheck="false"
                   class="form-control"
                   value="{{ $company->name }}"
            />

相关问题