简体   繁体   English

在Laravel 5上的Form ::复选框

[英]Form::checkbox on Laravel 5

I just started learning laravel and I have the following situation: 我刚刚开始学习laravel,并且遇到以下情况:

I have a table which is called 'cities' and a table 'users'. 我有一个称为“城市”的表和一个名为“用户”的表。 To connect them I have a table users_cities. 为了连接它们,我有一个表users_cities。

I am trying to do a form using laravel 我正在尝试使用laravel进行表单

@extends('welcome')

@section('content')

    <h1>Cities</h1>

    @foreach ($cities as $city)

    {!! Form::checkbox($city->name, $city->id, null, ['class' => 'field']) !!}{!! $city->name !!}

    @endforeach

    @foreach ($citiesSelect as $citySelect)

    {!! $citySelect->id !!}

    @endforeach 

@stop

And it`s bringing the name of the cities on the first foreach, and on the second is bringing the ids of the cities that should be checked. 它在第一个foreach中带来了城市的名称,在第二个中带来了应该检查的城市的ID。

Basically is bringing the entries on the database which has the id_user selected. 基本上是将条目带入已选择id_user的数据库中。

But I can't figure out how to bring the checkboxes on the first foreach selected using the result of the second foreach. 但是我不知道如何使用第二个foreach的结果来在第一个foreach中选中复选框。

只需在您的语句中添加一个“ true”参数,例如:

Form::checkbox($city->name, $city->id, true, ['class' => 'field'])

first create array of city ids like this 首先创建像这样的城市ID数组

$selectedCitiesIds = array_map(function($city) {
  return $city->id;
}, $citiesSelect);

then 然后

{!! Form::checkbox($city->name, $city->id, (in_array($city->id, $selectedCitiesIds)), ['class' => 'field']) !!}{!! $city->name !!}

You can do this too. 你也可以这样做。

In this case you have an array called cmbbolsas and in the checkbox input you can use it 在这种情况下,您有一个名为cmbbolsas的数组,可以在复选框输入中使用它

@foreach ($cmbbolsas as $key=>$value)
   <input type="checkbox" name="bolsas" value={{$key}}> {{$value}}</br>
@endforeach

I hope it works for you :3 希望它对您有用:3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM