常规错误:1364“发布”字段没有默认值

时间:2019-10-12 13:00:23

标签: php database laravel

我收到这样的错误,请帮忙解决,如果将其删除(“ published”,“ created_by”,“ modified_by”),则一切正常。如何清除此错误? 我会先感谢您的帮助 类别php

class category extends Model
{
     protected $fillable = ['title','slug','parent_id','published', 'created_by', 'modified_by'];


    public function setSlugAttribute($value){
        $this->attributes['slug'] = Str::slug( mb_substr($this->title, 0, 40) . "-" . \Carbon\Carbon::now()->format('dmyHi'), '-');
    }   
    public function children(){
        return $this->hasMany(self::class, 'parent_id');
    }
}

create_categories_table

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->integer('parent_id')->nulltable();
              $table->tinyinteger('published',0)->nulltable();
              $table->integer('created_by')->nulltable();
             $table->integer('modified_by')->nulltable();
             $table->timestamps();
        });

form.blade.php

<label for="published">Status</label>
<select id="published" class="form-control" name="published">

@if (isset($category) && isset($category->id))
    <option value="0" {{ ($category->published == 0) ? 'selected' : '' }}>None</option>
    <option value="1" {{ ($category->published == 1) ? 'selected' : '' }}>Okay</option>
@else
    <option value="0" selected disabled>None</option>
    <option value="1" disabled>Okay</option>
@endif
</select>

<label for="title">Name</label>
<input id="title" type="text" class="form-control" name="title" placeholder="Заголовок категории" value="{{ $category ? $category->title : '' }}" required>

<label for="slug">Slug</label>
<input id="slug" class="form-control" type="text" name="slug" placeholder="Auto" value="{{ $category ? $category->slug : '' }}" readonly />

<label for="parent_id">Parent</label>
<select id="parent_id" class="form-control" name="parent_id">
  <option value="0">-- None category --</option>
  @include('admin.categories.partials.categories', 
          ['categories' => $categories,
           'current' => $category,
           'delimiter' => $delimiter])
</select>

<hr />

<input class="btn btn-primary" type="submit" value="Save">

2 个答案:

答案 0 :(得分:0)

create_categories_table中替换为以下代码:

$table->integer('parent_id')->nullable();
$table->tinyinteger('published')->default(0)->nullable();
$table->integer('created_by')->nullable();
$table->integer('modified_by')->nullable();

答案 1 :(得分:0)

虽然您在表中保存了一些记录,但是您没有传递一些字段,所以它应该具有默认值,否则为

当您通过nulltable时,我在这里看到错误,应该是nullable

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->integer('parent_id')->nullable();
            $table->tinyinteger('published',0)->nullable();
            $table->integer('created_by')->nullable();
            $table->integer('modified_by')->nullable();
            $table->timestamps();
        });