Php Laravel-表单输入-一对多关系的多选

Php Laravel-表单输入-一对多关系的多选,php,forms,laravel,one-to-many,Php,Forms,Laravel,One To Many,我正在构建的应用程序中的一个要求是表单输入,它为单个字段接收不同数量的项。例如,我所做的运动有‘足球’、‘网球’、‘槌球’ 一个人可以参加的运动项目数量有限(可以说),因此这些项目应该从表单输入中的“下拉”类型列表中选择 此表单的下游是两个具有一对多关系的表。因此,从上面来看,“user”表只有一行,而“user\u sports”表有三行。然后通过用户表中的id字段链接这些数据 我在文档中找不到可以实现这一点的功能(也许我没有搜索正确的东西)。下面是我找到的最接近的,但仅用于从下拉列表中选择

我正在构建的应用程序中的一个要求是表单输入,它为单个字段接收不同数量的项。例如,我所做的运动有‘足球’、‘网球’、‘槌球’

一个人可以参加的运动项目数量有限(可以说),因此这些项目应该从表单输入中的“下拉”类型列表中选择

此表单的下游是两个具有一对多关系的表。因此,从上面来看,“user”表只有一行,而“user\u sports”表有三行。然后通过用户表中的id字段链接这些数据


我在文档中找不到可以实现这一点的功能(也许我没有搜索正确的东西)。下面是我找到的最接近的,但仅用于从下拉列表中选择单个项目


是否有一种变通方法可以让我使用Laravel框架启动并运行这个表单元素


或者,是否有其他方法可以在不损害用户体验的情况下实现此类功能?

多选实际上只是具有
multiple
属性的选择。考虑到这一点,它应该像

Form::select('sports[]', $sports, null, array('multiple'))
第一个参数只是名称,但在使用
Input::get('sports')
时,使用
[]
对其进行后期修复后,会将其作为数组返回

第二个参数是一组可选选项

第三个参数是要预先选择的选项数组


第四个参数实际上是通过将
multiple
属性添加到实际的select元素,将其设置为多选下拉列表。

我同意user3158900,我只是在使用方式上略有不同:

{{Form::label('sports', 'Sports')}}
{{Form::select('sports',$aSports,null,array('multiple'=>'multiple','name'=>'sports[]'))}}
但是,根据我的经验,select的第三个参数仅为字符串,因此,为了重新填充多重select的数据,我必须执行以下操作:

<select multiple="multiple" name="sports[]" id="sports">
@foreach($aSports as $aKey => $aSport)
    @foreach($aItem->sports as $aItemKey => $aItemSport)
        <option value="{{$aKey}}" @if($aKey == $aItemKey)selected="selected"@endif>{{$aSport}}</option>
    @endforeach
@endforeach
</select>

@foreach($aSports as$aKey=>$aSport)
@foreach($aItem->sports as$aItemKey=>$aItemSport)
{{$aSport}}
@endforeach
@endforeach

@SamMonk你的技术很棒。但您可以使用laravel form helper来执行此操作。我有一个客户和狗的关系

在你的控制器上

$dogs = Dog::lists('name', 'id');
在“客户创建”视图上,您可以使用

{{ Form::label('dogs', 'Dogs') }}
{{ Form::select('dogs[]', $dogs, null, ['id' => 'dogs', 'multiple' => 'multiple']) }}
第三个参数接受井的阵列列表。如果在模型上定义关系,则可以执行以下操作:

{{ Form::label('dogs', 'Dogs') }}
{{ Form::select('dogs[]', $dogs, $customer->dogs->lists('id'), ['id' => 'dogs', 'multiple' => 'multiple']) }}
更新Laravel 5.1

lists方法现在返回一个集合。

拉威尔4.2

@SamMonk给出了最好的替代方案,我按照他的例子构建了最后一段代码

<select class="chosen-select" multiple="multiple" name="places[]" id="places">
    @foreach($places as $place)
        <option value="{{$place->id}}" @foreach($job->places as $p) @if($place->id == $p->id)selected="selected"@endif @endforeach>{{$place->name}}</option>
    @endforeach
</select>
预选(编辑视图)

完全使用

{{ Form::chosen('places', $places, $job->places, ['multiple': false, 'title': 'I\'m a selectbox', 'class': 'bootstrap_is_mainstream']) }}

我的解决方案,它是使用jquery选择和引导生成的,id是用于jquery选择、测试和工作的,我在连接@foreach时遇到问题,但现在使用双@foreach和双@if:

  <div class="form-group">
    <label for="tagLabel">Tags: </label>
    <select multiple class="chosen-tag" id="tagLabel" name="tag_id[]" required>
      @foreach($tags as $id => $name)
        @if (is_array(Request::old('tag_id')))
                <option value="{{ $id }}" 
                @foreach (Request::old('tag_id') as $idold)
                  @if($idold==$id)
                    selected
                  @endif 
                @endforeach
                style="padding:5px;">{{ $name }}</option>
        @else
          <option value="{{ $id }}" style="padding:5px;">{{ $name }}</option>
        @endif
      @endforeach
    </select>
  </div>

如果您需要相互比较两个输出数组,但使用第一个数组填充选项,那么这可能是一种比top answer更好的方法

当数组中有非数字或偏移索引(键)时,这也很有用

<select name="roles[]" multiple>
    @foreach($roles as $key => $value)
        <option value="{{$key}}" @if(in_array($value, $compare_roles))selected="selected"@endif>
            {{$value}}
        </option>
    @endforeach
</select>

@foreach($key=>$value的角色)
{{$value}}
@endforeach

只有单if条件

<select name="category_type[]" id="category_type" class="select2 m-b-10 select2-multiple" style="width: 100%" multiple="multiple" data-placeholder="Choose" tooltip="Select Category Type">
 @foreach ($categoryTypes as $categoryType)
  <option value="{{ $categoryType->id }}"
    **@if(in_array($categoryType->id,
     request()->get('category_type')??[]))selected="selected"
    @endif**>
     {{ ucfirst($categoryType->title) }}</option>
     @endforeach
 </select>

@foreach($categoryTypes作为$categoryType)
身份证件
request()->get('category_type')??[])selected=“selected”
@endif**>
{{ucfirst($categoryType->title)}
@endforeach

我不知道是否可以将集合作为第一个参数传递。您是否应该使用
$customer->dogs->list('id')->toArray()
?这个答案是在Laravel 5发布之前给出的。当时(4.2节)默认情况下,
lists()
返回了数组。我认为这应该被标记为正确答案。你也可以在Dog模型上创建一个访问器,我认为它更干净一些。公共函数getDogListAttribute(){return$this->dogs->lists('id')->all();}然后在视图中,将dog_list[]属性与select form helper的第一个参数中的数组括号一起使用(第三个参数为空)。请注意,
lists
已重命名为
pulk
。方法签名是相同的,因此对于laravel 5.4,它将是
$customer->dogs->pulk('id')->all()
。这样如何:
@foreach($key=>$value)tags->pulk('id')->toArray()所选“:”}>{{$value}}@endforeach
{{ Form::chosen('places', $places, $job->places, ['multiple': false, 'title': 'I\'m a selectbox', 'class': 'bootstrap_is_mainstream']) }}
  <div class="form-group">
    <label for="tagLabel">Tags: </label>
    <select multiple class="chosen-tag" id="tagLabel" name="tag_id[]" required>
      @foreach($tags as $id => $name)
        @if (is_array(Request::old('tag_id')))
                <option value="{{ $id }}" 
                @foreach (Request::old('tag_id') as $idold)
                  @if($idold==$id)
                    selected
                  @endif 
                @endforeach
                style="padding:5px;">{{ $name }}</option>
        @else
          <option value="{{ $id }}" style="padding:5px;">{{ $name }}</option>
        @endif
      @endforeach
    </select>
  </div>
    $(".chosen-tag").chosen({
  placeholder_text_multiple: "Selecciona alguna etiqueta",
  no_results_text: "No hay resultados para la busqueda",
  search_contains: true,
  width: '500px'
});
<select name="roles[]" multiple>
    @foreach($roles as $key => $value)
        <option value="{{$key}}" @if(in_array($value, $compare_roles))selected="selected"@endif>
            {{$value}}
        </option>
    @endforeach
</select>
<select name="category_type[]" id="category_type" class="select2 m-b-10 select2-multiple" style="width: 100%" multiple="multiple" data-placeholder="Choose" tooltip="Select Category Type">
 @foreach ($categoryTypes as $categoryType)
  <option value="{{ $categoryType->id }}"
    **@if(in_array($categoryType->id,
     request()->get('category_type')??[]))selected="selected"
    @endif**>
     {{ ucfirst($categoryType->title) }}</option>
     @endforeach
 </select>