Reputation: 378
I just started learning laravel and I have the following situation:
I have a table which is called 'cities' and a table 'users'. To connect them I have a table users_cities.
I am trying to do a form using 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.
Basically is bringing the entries on the database which has the id_user selected.
But I can't figure out how to bring the checkboxes on the first foreach selected using the result of the second foreach.
Upvotes: 0
Views: 5131
Reputation: 103
You can do this too.
In this case you have an array called cmbbolsas and in the checkbox input you can use it
@foreach ($cmbbolsas as $key=>$value)
<input type="checkbox" name="bolsas" value={{$key}}> {{$value}}</br>
@endforeach
I hope it works for you :3
Upvotes: 0
Reputation: 2090
first create array of city ids like this
$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 !!}
Upvotes: 1
Reputation: 2179
simply add a 'true' argument to your statement like such:
Form::checkbox($city->name, $city->id, true, ['class' => 'field'])
Upvotes: 0