代码之家  ›  专栏  ›  技术社区  ›  Razvan Zamfir

如何在Laravel 8应用程序中显示多选的选定选项?

  •  0
  • Razvan Zamfir  · 技术社区  · 3 月前

    我正在Laravel 8中开发一个博客应用程序。

    在我用于编辑文章的表单中,我有一个多选元素。

    我希望表单显示已选择的标签。为此,我在视图中做了以下操作:

    <label for="tags" class="col-md-12">{{ __('Tags') }}</label>
    
    @php
      $selected = explode(",", $tags);
    @endphp
    
    <select name="tags[]" id="tags" class="form-control" multiple="multiple">
        @foreach ($tags as $tag)
            <option value="{{ $tag->id }}" {{ (in_array($tag->id, $selected)) ? 'selected' : '' }}>{{ $tag->name }}</option>
        @endforeach
    </select>
    

    在控制器中,我有:

    public function edit($id)
      {
        $article = Article::find($id);
        return view(
          'dashboard/edit-article',
          [
            'categories' => $this->categories(),
            'tags' => $this->tags(),
            'article' => $article
          ]
        );
      }
    
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), $this->rules, $this->messages);
    
        if ($validator->fails()) {
          return redirect()->back()->withErrors($validator->errors())->withInput();
        }
    
        $fields = $validator->validated();
        $article = Article::find($id);
    
        // If a new image is uploaded, set it as the article image
        // Otherwise, set the old image...
        if (isset($request->image)) {
          $imageName = md5(time()) . Auth::user()->id . '.' . $request->image->extension();
          $request->image->move(public_path('images/articles'), $imageName);
        } else {
          $imageName = $article->image;
        }
    
        $article->title = $request->get('title');
        $article->short_description = $request->get('short_description');
        $article->category_id = $request->get('category_id');
        $article->featured = $request->has('featured');
        $article->image = $request->get('image') == 'default.jpg' ? 'default.jpg' : $imageName;
        $article->content = $request->get('content');
        // Save changes to the article
        $article->save();
        //Attach tags to article
        if ($request->has('tags')) {
          $article->tags()->attach($request->tags);
        }
    
        return redirect()->route('dashboard.articles')->with('success', 'The article titled "' . $article->title . '" was updated');
    }
    

    由于我无法发现的原因,这不起作用。这意味着文章的标签在 <select> 元素。

    我的错误在哪里?

    1 回复  |  直到 3 月前
        1
  •  2
  •   Olumuyiwa    3 月前

    根据您的代码,我可以推断出tags变量是对象的集合。因此,您不能像php块语句中的刀片文件那样对其进行分解。根据其文档,php explosit函数接受“分隔符”和“字符串”作为参数 here 。如果您的目标是返回所选标签的id,则可以提取标签集合上的id列,并将其作为数组返回,如下所示。当然,如果“标签”确实是一个集合,否则你可以把它变成一个集合。

    $selected = $this->tags->pluck('id')->toArray(); // If tags is a collection
    

    但如果不是收藏,

    $tagsCollection = collect($this->tags);
    $selected = $tagsCollection->pluck('id')->toArray();
    

    之后,您可以将其与控制器中编辑方法中的其余数据一起传递给视图。因此,您在刀片文件中不再需要php块,所选标签现在应该可见。