【发布时间】:2019-09-10 21:59:16
【问题描述】:
我实际上是 Laravel 框架的新手。一直试图发布到 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;
}
【问题讨论】:
-
你必须像这样添加 nullable() 函数。 $table->integer('author_id')->unsigned()->nullable();
-
这个错误意味着你没有用
author_id填充数据。您的$fillable数组中也缺少此字段,或者您必须在迁移时将此字段指定为可为空。 -
是的,我知道我没有填写它,即使我将它添加到我的可填写项中,我仍然得到同样的错误。我是 Laravel Nerea 的新手,请指导我,谢谢。我应该在哪个页面添加 Safak Ciplak?
-
您必须将您创建的帖子与特定用户(又名作者)相关联。否则将 author_id 列设置为可为空,但这不是您想要的,因为帖子有作者
-
Nikos M 非常正确。在这种情况下,可空值不是最佳选择。作者必须对发表的每篇文章负责。如果你能帮助我,我将不胜感激。这个阶段真的消耗了我很多时间。提前致谢。
标签: php mysql eloquent laravel-5.5 laravel-5.8