Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQLSTATE[23000]:完整性约束冲突:1048列';类别标识';不能为空_Php_Laravel 5 - Fatal编程技术网

Php SQLSTATE[23000]:完整性约束冲突:1048列';类别标识';不能为空

Php SQLSTATE[23000]:完整性约束冲突:1048列';类别标识';不能为空,php,laravel-5,Php,Laravel 5,我想在post/create表单中用categery_id作为下拉列表创建一篇文章,但我得到了一个SQLSTATE[23000]:完整性约束冲突:1048列“category_id”不能为空(SQL:insert-intoposts(标题,正文,类别id,作者id,更新,创建) 我需要一点帮助来找出导致这个问题的原因,以下是我目前所掌握的 create.blade.php @extends('layouts.app') @section('content')

我想在post/create表单中用categery_id作为下拉列表创建一篇文章,但我得到了一个SQLSTATE[23000]:完整性约束冲突:1048列“category_id”不能为空(SQL:insert-into
posts
标题
正文
类别id
作者id
更新
创建

我需要一点帮助来找出导致这个问题的原因,以下是我目前所掌握的

create.blade.php

    @extends('layouts.app')
        @section('content')
         <div class="container">
         <h1>Create Post</h1>
          <div class="row post_row">

       {!! Form::open(['action' => 'PostsController@store', 'method' => 'POST', 'class' => 'form']) !!}
      <div class="col-md-8">           
         <div class'form-group'>
           {{ Form::label('title', 'Title')}}
           {{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title'])}}
         </div>
      </div>

      <div class="col-md-4">
        <div class'form-group'>
           {{ Form::label('categories_id', 'Category :')}}
             <select class='form-control' title="category_id">
               @foreach ($categories as $category => $value)
                <option value="{{$value->id }}">{{ $value->title }}</option>
               @endforeach
            </select>
        </div>
      </div>
</div>

<div class="row post_row">
   <div class="col-md-8">
     <div class'form-group'>
       {{ Form::label('body', 'Body')}}
       {{ Form::textarea('body', '', ['id' => 'article-ckeditor', 'class' => 'form-control space', 'placeholder' => 'Body Text'])}}
     </div>
  </div>
</div>

   <div class'form-group' style="padding-top: 20px">
      {{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
      {!! Form::close() !!}
  </div>
</div>  

@endsection
CreatePostsTable.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
   /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('author_id')->unsigned();
        $table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
        $table->string('title');
        $table->string('slug')->unique();
        $table->text('excerpt');
        $table->mediumText('body');
        $table->string('image')->nullable();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('posts');
}
}

到目前为止,我的问题是这样的,有人知道我为什么会有这个问题吗?

认为这个问题是因为category id在CreatePostsTable迁移中不可为null。您可以像这样使其为null

$table->integer('category_id')->nullable()->unsigned();

我在我的CreatePostsTable中做了这个修复,但我仍然得到[SQLSTATE[23000]:完整性约束冲突:1048列“category_id”不能为空(SQL:insert-into
posts
title
body
category_id
author_id
updated_at
)值(…]我想您必须再次迁移。删除表并运行migrate命令。好的,这起作用了,我现在可以在我的数据库thx中的post表上创建一个post。但是category_id显示为“null”在数据库中。我希望类别中的值替换空值。空值与类别表中的类别标题和帖子上的下拉列表相对应。创建?是的,您可以创建它们并检查laravel数据库种子。这是一个很好的了解区域,如果正确,您可以接受答案,那将非常好;)
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
protected $table = 'categories';

protected $fillable = [
    'title',
    'categery_id'
];

public function posts()
{
    return $this->hasMany(Post::class);
}

public function getRouteKeyName()
{
    return 'slug';
}
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
   /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('author_id')->unsigned();
        $table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
        $table->string('title');
        $table->string('slug')->unique();
        $table->text('excerpt');
        $table->mediumText('body');
        $table->string('image')->nullable();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('posts');
}
}
$table->integer('category_id')->nullable()->unsigned();