I'm actually new to Laravel Framework. Have been trying to post to the Mysql Database, I was getting an error on my Author_id.
SQLSTATE[HY000]: General error: 1364 Field 'author_id' doesn't have a default value.
Have checked everywhere but all efforts were proven abortive
Here is the Migration
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();
});
}
Here is my 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>
Here is my Post Model:
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;
}
author_id
. Also this field is missing from your$fillable
array or you must specify this field as nullable on your migration. – Nerea