我想使用Laravel 5.4发布帖子即使选中的项目也会出现所需的错误。
这是我的邮政控制器的商店部分:
public function store(Request $request)
{
// validate the date
$this->validate($request, array(
'title' => 'required|max:255',
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
'category_id' => 'required|integer',
'body' => 'required',
'tags' => 'sometimes',
'featured_image' => 'sometimes|image'
));
// store in the database
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->category_id = $request->category_id;
$post->body = $request->body;
$post->tags_id = $request->tags_id;
//Save featured image
if($request->hasFile('featured_image')) {
$image = $request->file('featured_image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('uploads/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$post->image = $filename;
}
$post->save();
$post->tags()->sync($request->tags, false);
Session::flash('success', 'The blog post was successfully save!');
// redirect to anther page
return redirect()->route('posts.show', $post->id);
}
这是我的表格:
{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '', 'files' => true)) !!}
<div class="col-md-8">
<!-- <div class="panel"> -->
<!-- title -->
<div class="panel-heading">
<i class="fa fa-pencil"></i> {{ Form::label('title', 'Title:') }}
</div>
<div class="panel-body">
{{ Form::text('title', null, array('class' => 'form-control input-lg', 'required' => '', 'maxlenght' => '255')) }}
</div>
<!-- slug -->
<div class="panel-heading">
<i class="fa fa-link"></i> {{ Form::label('slug', 'Slug:') }} <small>hello_world. Use(_) between words.</small>
</div>
<div class="panel-body">
{{ Form::text('slug', null, array('class' => 'form-control', 'required' => '', 'minlenght' => '5','maxlenght' => '255')) }}
</div>
<!-- Category -->
<div class="panel-heading">
<i class="fa fa-list-ol"></i> {{ Form::label('category_id', 'Category:') }}
</div>
<select class="form-control" name="category">
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
</select>
<!-- body -->
<div class="panel-heading">
<i class="fa fa-book"></i> {{ Form::label('body', 'Post Body:') }}
</div>
<div class="panel-body">
{{ Form::textarea('body', null, array('class' => 'form-control', 'required' => '', 'id' => 'bodytext')) }}
</div>
<!-- Tags -->
<div class="panel-heading">
<i class="fa fa-tags"></i> {{ Form::label('tags', 'Tags:') }}
</div>
<select class="form-control select2-multi" name="tags[]" multiple="multiple">
@foreach($tags as $tag)
<option value="{{ $tag->id }}">{{ $tag->name }}</option>
@endforeach
</select>
</div>
<div class="col-md-4">
<div class="panel panel panel-bordered panel-info">
<!-- image -->
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-image"></i> {{ Form::label('featured_image', 'Upload Featured Image') }}</h3>
<div class="panel-actions">
<a class="panel-action" data-toggle="panel-collapse" aria-hidden="true"></a>
</div>
</div>
<div class="panel-body">
{{ Form::File('featured_image') }}
</div>
</div>
<!-- button -->
<div class="panel panel panel-bordered panel-warning">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-clipboard"></i> Actions</h3>
<div class="panel-actions">
<a class="panel-action" data-toggle="panel-collapse" aria-hidden="true"></a>
</div>
</div>
<div class="panel-body">
{{ Form::submit('Publish it', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }}
</div>
</div>
</div>
{!! Form::close() !!}
我不知道问题究竟在哪里,任何想法?
答案 0 :(得分:1)
在视图中,输入字段的名称为category
。但是在控制器中,您正在检查category_id
。
<强>建议:强>
为什么不为select
字段使用 LaravelCollective HTML包?
{!! Form::select(
'category_id',
$category_list,
null,
['class' => 'form-control']
) !!}
在create
函数中的控制器中。
$category_list = Category::lists('id', 'name')->all();
将此变量传递给视图。