Laravel表单构建器多选所选项目

时间:2017-08-27 10:39:49

标签: forms laravel-5 laravel-5.4 formbuilder laravelcollective

在多重选择中显示所选项目时出现问题。

在'index.blade.php'实体视图中有一个'filter.blade.php'子视图

子视图片段,表单输入,多个多选项之一:

public function filter(Request $request)
{
    $this->validate($request, $this->filterRules);
    $filters = [
        'category_id'     => (int) $request->category_id,
        'subcategories'   => $request->subcategories,
        'grades'          => $request->grades,
        'students'        => $request->students,
        'professors'      => $request->professors,
    ];

    $works = ArtWork::filtered($filters);
    $subcategories = Category::where('category_id', $filters['category_id'])
        ->get()
        ->pluck('title', 'id')
        ->toArray();

    return view('panel.art-work.index', [
        'works'           => $works,
        'subcategories'   => $subcategories,

        '_category'       => $filters['category_id'],
        '_subcategories'  => $filters['subcategories'],
        '_grades'         => $filters['grades'],
        '_students'       => $filters['students'],
        '_professors'     => $filters['professors'],

        '_filter'         => true,
    ]);
}

控制器操作,处理来自请求的输入以过滤结果

<?php
$hit_count = @file_get_contents('count.txt'); // read the hit  count      from file
 echo $hit_count; //  display the hit count
$hit_count++; // increment the hit count by 1
@file_put_contents('count.txt', $hit_count); // store the new hit count
?>

因此,当过滤器表单提交给控制器并显示带有“_subcategories”等变量的视图时,应该从多选中的请求中选择ID。但没有选择。

http://joxi.ru/82QeLzDi135Xy2

很奇怪,我卡住了......

由于

1 个答案:

答案 0 :(得分:0)

我的工作解决方案是:

字符串化每个选项值(在我的情况下为ID)。

在html中,而不是

<select>
   <option value="1">First</option>
   <option value="2">Second</option>
   ...
   <option value="n">Nth</option>

<select>

应该是

<select>
       <option value="d1">First</option>
       <option value="d2">Second</option>
       ...
       <option value="dn">Nth</option>

<select>

因此,在为表单构建器多选择传递具有键值对的数组之前,我添加了字母,例如,'d'为value。

并且,在使用所选项目提交表单后,我会在处理此数据之前清除每个值并删除字母