【问题标题】:SQL integrity constraint error in laravel 5.6laravel 5.6中的SQL完整性约束错误
【发布时间】:2018-12-16 18:46:55
【问题描述】:

目前正在使用 laravel 5.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();
    });
}

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

【问题讨论】:

  • 您能否在您的 PHP 中显示您正在处理此表单的部分?这可能是部分原因。

标签: php mysql laravel-5.6


【解决方案1】:

(请注意,包含您的 PHP 代码 sn-p 后,我会立即更新我的答案,因为这可能是您的错误的部分原因。)

您在输入元素中使用了错误的名称属性。您现在正在检查两次 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 }}"
            />

【讨论】:

  • 欢迎您!在这种情况下,您能接受并支持我的回答吗? :) 这就是 Stack Overflow 的工作原理:stackoverflow.com/help/someone-answers
  • Virginia 很抱歉我不能投票,因为我还没有 15 个声望
  • 别担心,接受答案也很好,因为这将帮助其他有同样问题的程序员:) 谢谢你的接受!
【解决方案2】:

您输入的属性名称不正确

<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 可以为空

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

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2019-04-12
    相关资源
    最近更新 更多