'Use array_filter in foreach
I am presenting this problem when making a store in my database, which is data entered from a table in my view.
Example: table
For example, my user selects row 1 and 4 in which he fills in the data and that is sent to the controller to be stored, the problem arises when filtering the values of the select and input columns.
values that the table sends: values
I need to store the values of the rows, for example 1 and 4, filtering the empty values that the user has not filled or selected
This is my view:
<table id="example4" class="display min-w850">
<thead>
<tr>
<th style="text-align: center;vertical-align: middle">Seleccionar</th>
<th style="text-align: center;vertical-align: middle">Software</th>
<th style="text-align: center;vertical-align: middle">Marca</th>
<th style="text-align: center;vertical-align: middle">Nombre de Version</th>
<th style="text-align: center;vertical-align: middle"> # Version</th>
<th style="text-align: center;vertical-align: middle">Tipo de venta</th>
<th style="text-align: center;vertical-align: middle">Observación</th>
</tr>
</thead>
<tbody>
@foreach ($vsoftware2 as $cli)
<tr>
<td style="text-align: center;vertical-align: middle">
<input type="checkbox" name="id_version[]" value="{{ $cli->id_version }}">
</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->nom_soft }}
</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->marca }}</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->nombre_ver }}
</td>
<td style="text-align: center;vertical-align: middle">{{ $cli->num_version }}
</td>
<td style="text-align: center;vertical-align: middle">
<select class="mr-sm-2" id="inlineFormCustomSelect"
name="id_tipo_venta[]">
<option value="" selected>Tipo de venta...</option>
<option value="1">Key</option>
<option value="2">Instalacion</option>
<option value="3">Key + Instalacion</option>
<option value="3">Viruz</option>
</select>
</td>
<td style="text-align: center;vertical-align: middle">
<input name="obs_software[]" class=" form-control" type="text"
autocomplete="off">
</td>
</tr>
@endforeach
</tbody>
</table>
this is my controller:
$checked_array = $request->input('id_version');
// $obs_software = ($request->input('obs_software'));
foreach ($_POST['id_version'] as $key => $value)
{
if (in_array($_POST['id_version'][$key], $checked_array)[$key])
{
$soft_instal = new Software_instalacion;
$soft_instal->id_instalacion = $instalaciones->id;
$soft_instal->id_historial = $historial->id;
$soft_instal->id_usuario = $request->id_usuario;
$soft_instal->id_version = $request->input('id_version')[$key];
$soft_instal->obs_software = $request->input('obs_software')[$key];
$soft_instal->id_tipo_venta = $request->input('id_tipo_venta')[$key];
$soft_instal->save();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|