'Insert multiple records at once from a table
I have this table which is loaded with data from the database and what the user has to do is select the one they want and fill in the data from both the select and the input, and that data can be saved in separate records. It is the first time I work with this so I don't know a bit about the process.
Currently my view I have it this way
<tbody>
@foreach($vsoftware as $cli)
<tr>
<td style="text-align: center;vertical-align: middle">
<input type="checkbox" name="id_software[]" value="{{$cli->id_software}}">
</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>sale...</option>
<option value="1">Key</option>
<option value="2">Instal</option>
<option value="3">Key + Instal</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">
</td>
</tr>
@endforeach
my controller I try to form it like this:
$soft_instal = new Software_instalacion;
$soft_instal->id_instalacion = $instalaciones->id;
$soft_instal->id_historial = $historial->id;
$soft_instal->id_software = $request->id_software;
$soft_instal->id_usuario = $request->id_usuario;
$soft_instal->id_tipo_venta = $request->id_tipo_venta;
$soft_instal->obs_software = $request->obs_software;
dd($soft_instal);
$soft_instal->save();
The answer that the dd gives me is: response
And the idea is that you can store it like this in the database: way to save
Solution 1:[1]
The simplest way is that you can loop through your checkbox array.
Here check out the @Tim Lewis answer: submit multiple arrays from html and combine their attribute into seperate collections and store in Laravel App
Also read about Laravel Mass Assignment: https://laravel.com/docs/8.x/eloquent#mass-assignment
Solution 2:[2]
you can use
foreach($request->input('id_software') as $index => $item) {
$soft_instal = new Software_instalacion;
$soft_instal->id_instalacion = $instalaciones->id;
$soft_instal->id_historial = $historial->id;
$soft_instal->id_software = $item;
$soft_instal->id_usuario = $request->input('id_usuario')[$index];
$soft_instal->id_tipo_venta = $request->input('id_tipo_venta')[$index];
$soft_instal->obs_software = $request->input('obs_software')[$index];
$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 |
---|---|
Solution 1 | Muhammad Akif Saleem |
Solution 2 | Hossein Azad |