我使用select2 jquery插件和laravel表单模型绑定来呈现来自服务器的数据。 尽管其他一切正常,但它不会将已附加到帖子的标签呈现为选定选项。选择2不显示所选选项。 (Laravel)
必须有我不知道的东西,这是我的观点部分。
<div class="form-group">
{!! Form::label('tag_list','Tags:') !!}
{!! Form::select('tag_list[]', $tags,null,['id'=>'tag_list', 'class'=>'form-control','multiple']) !!}
</div>
// This is the select 2 script
$('#tag_list').select2({
'placeholder':'Choose Tags',
tags:true,
tokenSeparators:[",", " "],
createTag:function(newTag){
return{
id:'new:' + newTag.term,
text:newTag.term + '(new)'
};
}
});
这是第一条模型getTagListAtrribute功能
//这是getTagListAttribute功能
public function getTagListAttribute(){
return $this->tags->lists('post_id')->all();
}
我从控制器加载编辑表单是这样的:
public function article_edit($slug){
// fetch the articles.
//$article = DB::table('articles')->where('slug',$slug)->first();
$article = Article::where('slug',$slug)->first();
/*echo '<pre>';
print_r($article->title);
die();*/
$tags = DB::table('tags')->lists('name','tag_id');
$categories=DB::table('categories')->lists('category_name','category_id');
return view('admin.pages.edit', compact('article','tags','categories'));
}
我只想要选择与文章相关的标签,而t他加载页面,而我一直无法。所以我需要帮助。
这可能只是我不明显,但你在哪里设置值选择? – apokryfos
表单模型绑定应该照顾到,当我使用null据我所知。 – user2906838
如果你在代码片段中也有'Form :: model'代码,那么它可能会有帮助。 – apokryfos