我目前正在为我的网站开发一个简单的博客系统,我正在使用Laravel5.5,但是我遇到了一个问题。每当我使用我创建的表单创建一篇新文章时,它都是在我的数据库中存储一个空行,而不是从表单中存储数据.
我的功能是存储一篇文章:
public function newsStore()
{
$input = Request::all();
Article::create($input);
return $input;
}根据我所遵循的教程,如果这样做的话..这是我目前使用的表单:
{!! Form::open(['url' => 'news']) !!}
<!-- Article Title -->
<div class="form-group row">
{!! Form::label('title', 'Title:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Description -->
<div class="form-group row">
{!! Form::label('description', 'Description:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('description', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Excerpt -->
<div class="form-group row">
{!! Form::label('excerpt', 'Excerpt:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::text('excerpt', null, ['class' => 'form-control']) !!}
</div>
</div>
<!-- Article Image -->
<div class="form-group row">
{!! Form::label('image', 'Image:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::file('image') !!}
</div>
</div>
<!-- Article Message -->
<div class="form-group row">
{!! Form::label('message', 'Message:', ['class' => 'col-2 col-form-label']) !!}
<div class="col-10">
{!! Form::textarea('message', null, ['class' => 'form-control', 'rows' => '7']) !!}
</div>
</div>
<!-- Article Submit -->
<div class="form-group">
{!! Form::button('Add Article', ['class' => 'btn btn-primary waves-effect waves-light float-right', 'type' => 'submit']); !!}
</div>
{!! Form::close() !!}这是我使用的路线:
Route::post('news', 'DashboardController@newsStore'); // The articles store route这是我的模型:
class Article extends Model
{
protected $fillable = [
'title',
'description',
'excerpt',
'image',
'body',
'published_at'
];
}我以前做过一次,试着重新做一遍,但似乎找不出哪里出了问题。
提前感谢!
发布于 2017-10-25 06:47:57
要使create()工作,请确保使用的列名称相同。例如,如果您的列名是title,则需要在表单中使用title而不是news-title:
{!! Form::text('title', null, ['class' => 'form-control']) !!}发布于 2017-10-25 07:07:31
在使用此Article::create();时,它意味着所有输入名称都应该与表列名相同。
Ex:
<input name="title" ...与表中的列调用news-title相同
如果不一样
使用查询生成器
DB::table('table_name')->insert(
[
'name' => $input->news-title
.....
]
);阅读更多关于MySQL DB中列名中的连字符的信息
https://stackoverflow.com/questions/46925646
复制相似问题