Unable to save arrays with the correct key based on how many inputs the user selects.
I have tried the following:
I've tried to json encode some elements, but to no avail.
Here's the html:
<div class="row">
<div class="col">
<div class="form-group">
<label>Block Name</label>
<input type="text" name="name_of_blocks[]" class="form-control">
</div>
</div>
<div class="col">
<label>No of units in the block</label>
<input type="number" name="no_units[]" class="form-control" id="blockNo">
</div>
<div class="col">
<label>Number of floors</label>
<input type="number" class="form-control" name="no_of_floors[]">
</div>
<div class="col">
<label>Does this block have a lift?</label>
<select class="form-control" name="lift[]">
<option>Please Select</option>
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
<div class="row">
<div class="col">
<label>What facilities does this block have?</label>
<div class="row">
<div class="col">
<ul class="list-unstyled">
<li><input type="checkbox" name="facilities[]" value="fob"> Fob Entry</li>
<li><input type="checkbox" name="facilities[]" value="gate_entry"> Gate Entry</li>
<li><input type="checkbox" name="facilities[]" value="key"> Key Entry</li>
<li><input type="checkbox" name="facilities[]" value="finger"> Fingerprint Entry</li>
<li><input type="checkbox" name="facilities[]" value="Code Entry"> Code Entry</li>
<li><input type="checkbox" name="facilities[]" value="mailbox"> Mailbox Area</li>
</ul>
</div>
<div class="col">
<ul class="list-unstyled">
<li><input type="checkbox" name="facilities[]" value="bar"> Bar</li>
<li><input type="checkbox" name="facilities[]" value="bike_storage"> Bike Storage</li>
<li><input type="checkbox" name="facilities[]" value="car_park"> Secure Car Parking</li>
<li><input type="checkbox" name="facilities[]" value="off_road"> Off-road Parking</li>
<li><input type="checkbox" name="facilities[]" value="visitor_parking"> Visitor Parking</li>
<li><input type="checkbox" name="facilities[]" value="concierge"> Concierge</li>
</ul>
</div>
<div class="col">
<ul class="list-unstyled">
<li><input type="checkbox" name="facilities[]" value="gym"> Gym</li>
<li><input type="checkbox" name="facilities[]" value="pool"> Swimming Pool </li>
<li><input type="checkbox" name="facilities[]" value="snooker"> Snooker/Pool Table</li>
<li><input type="checkbox" name="facilities[]" value="cafe"> Cafe</li>
<li><input type="checkbox" name="facilities[]" value="maintenance"> Maintenance Service</li>
<li><input type="checkbox" name="facilities[]" value="various"> Various businesses/commercial outlets</li>
</ul>
</div>
Heres what I have in my controller, but this either doesn't save correctly or saves one row if there's two and doesn't save the correct info:
$data = $request->all();
$count = $data['number_of_blocks'];
for ($i = 0; $i < $count; $i++) {
$fac = array_values($request->facilities);
$facil = json_encode($fac);
$blok = new Blocks;
$blok->block_id = $block->id;
$blok->name_of_blocks = array_key_exists($i, $data['name_of_blocks']) ? $data['name_of_blocks'][$i] : null;
$blok->no_units = array_key_exists($i, $data['no_units']) ? $data['no_units'][$i] : null;
$blok->no_of_floors = array_key_exists($i, $data['no_of_floors']) ? $data['no_of_floors'][$i] : null;
$blok->lift = array_key_exists($i, $data['lift']) ? $data['lift'][$i] : null;
$blok->facilities = array_key_exists($i, $facil) ? $facil[$i] : null;
$blok->save();
}
I do have some clone script to clone the amount of form fields based on how many the user wants, so this could also be a problem, but I'm not sure what or where:
$("#continue").click(function () {
$("#first").hide();
$("#second").show();
var blocks = $("#noBlocks").val();
var name = $("#blockName").val();
$(".clone").hide();
var select = parseInt(blocks, 10);
var $clone = $('.clone').html();
var html = '';
while (select > 0) {
html += $clone;
select--;
}
$("#area").empty().html(html);
$("#bno").html(blocks);
$("#bn").html(name);
});
Basically if the user selects 2 then I want the code to save two rows and the facilities to be json encoded for each row, and match what the user selects. Any help advice greatly appreciated.