如何在 laravel 的选择选项值中显示选定的标签?
Posted
技术标签:
【中文标题】如何在 laravel 的选择选项值中显示选定的标签?【英文标题】:How to display selected tags in select option value in laravel? 【发布时间】:2018-11-29 09:40:08 【问题描述】:这是我的帖子创建视图
<div class="col-lg-12">
<form action=" route('admin.post.store') " enctype="multipart/form-data" method="post">
csrf_field()
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" class="form-control" value=" old('title') " name="title" id="title"
placeholder="Enter Post Title">
<span class="text-danger"> $errors->first('title') </span>
</div>
<div class="form-group">
<label for="slug">Post Image</label>
<input type="file" class="form-control" name="image" id="image"
placeholder="Select Post Image">
<span class="text-danger"> $errors->first('image') </span>
</div>
<div class="form-group">
<label for="tags">Select Tags</label>
<select multiple class="form-control" name="tags[]" id="tags">
@foreach($tags as $id => $name)
<option id=" $id "> $name </option>
@endforeach
</select>
</div>
<div class="form-group">
<textarea class="body" name="body"> old('body') </textarea>
<span class="text-danger"> $errors->first('body') </span>
</div>
<div class="form-group">
<label for="category">Select Category</label>
<select class="form-control" name="category" id="category">
<option value="">Select</option>
@foreach($cats as $cat)
<option value=" $cat->id "> $cat->name </option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-success btn-block">Publish</button>
</form>
</div>
标签有多对多的关系。 在这里我可以选择很多标签,但是在帖子编辑视图中我看不到我在帖子创建视图中选择的标签。 我想在选择选项值和编辑主题中显示选定的标签。
发布更新方法:
public function edit(Post $post)
$tags = Tag::all()->pluck('name', 'id');
$cats = Category::all();
return view('admin.post.edit', compact(['post', 'cats', 'tags']));
public function update(Post $post, Request $request)
$this->validate($request, [
'title' => 'required|min:3|max:255',
'slug' => 'nullable|string',
'image' => 'sometimes|mimes:jpeg,bmp,png,jpg,gif',
'body' => 'required',
'category' => 'nullable',
'views' => 'nullable',
'tags' => 'nullable',
]);
$post->title = $request->title;
$post->slug = $request->slug;
$post->body = $request->body;
$post->category_id = $request->category;
if ($request->hasFile('image'))
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$oldfilename = $post->image;
$post->image = $filename;
Storage::delete($oldfilename);
$post->save();
$post->tags()->sync($request->tags, false);
Session::flash('update', 'Post Updated Successfully');
return redirect()->route('admin.post.index');
发布编辑视图:
<div class="col-lg-12">
<form action=" route('admin.post.update',$post->id) " enctype="multipart/form-data" method="post">
csrf_field()
method_field('patch')
<div class="form-group">
<label for="title">Post Title</label>
<input type="text" class="form-control" value=" $post->title " name="title" id="title"
placeholder="Enter Post Title">
<span class="text-danger"> $errors->first('title') </span>
</div>
<div class="form-group">
<label for="slug">Post Image</label>
<input type="file" class="form-control" name="image" id="image"
placeholder="Select Post Image">
<span class="text-danger"> $errors->first('image') </span>
</div>
<div class="form-group">
<label for="tags">Select Tags</label>
<select class="js-example-basic-single form-control" name="tags[]" id="tags" multiple="multiple">
</select>
</div>
<div class="form-group">
<textarea class="body" name="body"> $post->body </textarea>
<span class="text-danger"> $errors->first('body') </span>
</div>
<div class="form-group">
<label for="category">Select Category</label>
<select class="form-control" name="category" id="category">
<option value="">Select</option>
@foreach($cats as $cat)
<option <?php if ($cat->id == $post->category_id)
echo 'selected';
?> value=" $cat->id "> $cat->name </option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary btn-block">Edit</button>
</form>
</div>
这里:
<div class="form-group">
<label for="tags">Select Tags</label>
<select multiple class="form-control" name="tags[]" id="tags">
@foreach($tags as $id => $name)
<option id=" $id "> in_array($id,$post->category()->pluck('id')->toArray()) ? 'selected' : '' </option>
@endforeach
</select>
</div>
如何从数据库中传递选定的帖子标签并在此处显示主题并进行编辑。
我正在使用 select2 插件
在 select2 中我应该在 jQuery 中传递数据:
$('.js-example-basic-single').select2().val( json_decode($post->tags()->getRelatedIds()) ).trigger('change');
但它不起作用:(
我得到了这个错误
方法 Illuminate\Database\Query\Builder::getRelatedIds 不存在。 (查看:C:\Users\M0RT3Z4\Desktop\MyBlog\resources\views\admin\post\edit.blade.php
请帮助我,谢谢!
【问题讨论】:
【参考方案1】:你把代码放错地方了:
试试这个:
<div class="form-group">
<label for="tags">Select Tags</label>
<select multiple class="form-control" name="tags[]" id="tags">
@foreach($tags as $id => $name)
<option id=" $id " in_array($id,$post->category()->pluck('id')->toArray()) ? 'selected' : '' > $name </option>
@endforeach
</select>
</div>
【讨论】:
这项工作 tnx 但它只是选择第一个标签我想它选择相关标签 也许将$post->category()->pluck('id')->toArray()
移出循环以减少占用空间?以上是关于如何在 laravel 的选择选项值中显示选定的标签?的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 4 如何使用 input::old() 方法在下拉菜单中显示选定的值?