SQLSTATE [HY000]:常规错误:1364字段“ author_id”没有默认值

时间:2019-04-21 10:44:33

标签: php mysql eloquent laravel-5.5 laravel-5.8

我实际上是Laravel Framework的新手。一直试图发布到Mysql数据库,我的Author_id出现错误。

  

SQLSTATE [HY000]:常规错误:1364字段'author_id'没有默认值。

已经检查了所有地方,但所有努力均被证明具有堕胎性

这是迁移

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        //$table->engine = “InnoDB”;

        $table->increments('id');
        $table->integer('author_id')->unsigned();
        $table->foreign('author_id')->unique()->references('id')->on('users')->onDelete('restrict');
        $table->string('title');
        $table->string('slug')->unique();
        $table->text('excerpt');
        $table->text('body');
        $table->unsignedInteger('user_id');
        $table->string('image')->nullable();   
        $table->timestamps();
    });



}

这是我的create.blade.php:

 <section class="content">
        <div class="row">
          <div class="col-xs-12">
            <div class="box">
              <div class="box-body">

              {!! Form::model($post, [
                    'method' => 'POST',
                    'url'  => 'backend/blog'
              ]) !!}  

             {{-- <form method="POST" action="{{ url('blog/store') }}" enctype="multipart/form-data">
                @csrf  --}}

                    <div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
                        {!! Form::label('title') !!}
                        {!! Form::text('title', null, ['class' => 'form-control']) !!}

                        @if($errors->has('title'))
                        <span class="help-block">{{ $errors->first('title') }} </span>
                        @endif
                    </div>


                    <div class="form-group {{ $errors->has('slug') ? 'has-error' : '' }}">
                      {!! Form::label('slug') !!}
                      {!! Form::text('slug', null, ['class' => 'form-control'])!!}

                      @if($errors->has('slug'))
                      <span class="help-block">{{ $errors->first('slug') }} </span>
                      @endif
                      </div>

                        <div class="form-group {{ $errors->has('excerpt') ? 'has-error' : '' }}">
                          {!! Form::label('excerpt') !!}
                          {!! Form::textarea('excerpt', null, ['class' => 'form-control'])!!}

                        @if($errors->has('excerpt'))
                        <span class="help-block">{{ $errors->first('excerpt') }} </span>
                        @endif

                        </div>

                            <div class="form-group {{ $errors->has('body') ? 'has-error' : '' }}">
                              {!! Form::label('body') !!}
                              {!! Form::textarea('body', null, ['class' => 'form-control'])!!}

                        @if($errors->has('body'))
                        <span class="help-block">{{ $errors->first('body') }} </span>
                        @endif
                            </div>

                          <div class="form-group {{ $errors->has('published_at') ? 'has-error' : '' }}">
                            {!! Form::label('published_at', 'Published Date') !!}
                            {!! Form::text('published_at', null, ['class' => 'form-control', 'placeholder' => 'Y-m-d H:i:s'])!!}

                            @if($errors->has('published_at'))
                            <span class="help-block">{{ $errors->first('published_at') }} </span>
                            @endif
                            </div>

                        <div class="form-group {{ $errors->has('category_id') ? 'has-error' : '' }}">
                          {!! Form::label('category_id', 'Category') !!}
                          {!! Form::select('category_id', App\Category::pluck('title', 'id'), null, ['class' => 'form-control', 'placeholder' => 'Choose category' ]) !!}

                          @if($errors->has('category_id'))
                          <span class="help-block">{{ $errors->first('category_id') }} </span>
                          @endif
                        </div>

                        <hr>

                        {!! Form::submit('Create new post', ['class' => 'btn btn-primary']) !!}
                {!! Form::close() !!}
              </div>
              <!-- /.box-body -->
            </div>
            <!-- /.box -->
          </div>
        </div>
      <!-- ./row -->
    </section>

这是我的帖子模型:

class Post extends Model

{

protected $fillable = ['title', 'slug', 'excerpt', 'body', 'user_id', 'published_at', 'category_id'];
protected $dates = ['published_at'];

public function author()
{
    return $this->belongsTo(User::Class);
}

public function category()
{
    return $this->belongsTo(Category::class);
}
// images path
Public function getImageUrlAttribute($Value)
{
    $imageUrl = "";

if ( ! is_null($this->image))
{
    $imagePath = public_path(). "/img/" . $this->image;
    if (file_exists($imagePath)) $imageUrl = asset("img/" . $this->image);
}
return $imageUrl;

}

1 个答案:

答案 0 :(得分:0)

例如,将Post与当前用户相关联(例如,在将Post保存到db的控制器方法内部)可以完成以下操作:

// controller method
public function savePost( Request $request )
{
    $data = $request->validate([
        // your validation rules here..
        ]);
    $post = new Post(); // from Post model
    $post->fill($data);
    $post->author()->associate(\Auth::user()); // relate post to current user
    $post->save(); // save to db
    return view('whatever_your_view_is', []);
}

如果您需要更多信息,请发表评论

Post模型中进行以下更改:

public function author()
{
    return $this->belongsTo(User::Class, 'author_id'); // specify the column which stores the author in posts table
}

请参见documentation on defining relationships