UPDATED
I made some changes to my controller and form, I still can't insert an insert associated with a tool
Within my form I have a table that allows you to dynamically add / delete input fields. In this same table I can insert as many tools as I want, I want to clarify in this same table 2 entities participate (TOOL-INSERT), with a one-to-many relationship.
When trying to save the form I receive the following error that comes from my dynamic table:
message: "Array to string conversion (SQL: insert into tools (code_tool, insert_id, position) values (www, effeee, 01))"
The doubts I have are the following:
How to capture the multiple data entered in my form through my pivot table?
How to store multiple data that comes in array format?
next my models, my form and my controller
model Tool:
use Illuminate\Database\Eloquent\Model;
class Tool extends Model
{
protected $fillable = [
'insert_id', 'position', 'code_tool', 'type', 'category', 'status', 'description', 'reason'
];
public function inserts()
{
return $this->hasMany(Insert::class);
}
public function pieces()
{
return $this->belongsToMany(Piece::class)->withTimestamps();
}
}
model Insert:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Insert extends Model
{
protected $fillable = [
'code_insert', 'quality', 'type', 'category', 'status', 'description', 'reason'
];
public function tool()
{
return $this->belongsTo(Insert::class);
}
}
form:
<div class="row">
{!! Form::model($piece, [
'route' => $piece->exists ? ['admin.pieces.update', $pieces->id] : 'admin.pieces.store',
'method' => $piece->exists ? 'PUT' : 'POST'
]) !!}
<div class="col-md-6">
<div class="box box-primary">
<div class="box-header with-border ">
<h3 class="box-title">Detalles del Legajo</h3>
</div>
<div class="box-body">
<table class="table table-striped table-bordered table-condensed table-hover" id="dynamicTable">
<tr>
<th>Posición</th>
<th>Herramienta</th>
<th>Inserto</th>
<th>Action</th>
</tr>
<tr>
<td><input type="text" name="position[]" placeholder="Posicion" class="form-control select2" /></td>
<td><input type="text" name="code_tool[]" placeholder="Codigo" class="form-control" /></td>
<td><input type="text" name="code_insert[]" placeholder="Codigo" class="form-control" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success"><i class="fa fa-plus-square"></i> Add More</button></td>
</tr>
</table>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
In my controller in the store method I am trying to traverse the array as follows:
controller:
public function store(Request $request)
{
if ($request->ajax()){
try {
// Transacciones
DB::beginTransaction();
$position = $request->position;
$code_tool = $request->code_tool;
$code_insert = $request->code_insert;
$quality = $request->quality;
for($count = 0; $count < count($position); $count++)
{
$insert = array(
'code_insert' => $code_insert[$count],
'quality' => $quality[$count]
);
$insert_data[] = $insert;
$tool = array(
'position' => $position[$count],
'code_tool' => $code_tool[$count],
'insert_id' => $insert_data[$count]
);
$tool_data[] = $tool;
}
dd($tool_data);
Insert::insert($insert_data);
Tool::insert($tool_data);
DB::commit();
} catch (Exception $e) {
// anula la transacion
DB::rollBack();
}
}
}
Adding a dd ($ tool_data); I inspect the console precisely in pestana network, I get the following:
array:1 [
0 => array:3 [
"position" => "01"
"code_tool" => "www"
"insert_id" => array:2 [
"code_insert" => "effeee"
"quality" => "de3"
]
]
]
my problem is not knowing if I am going through the array correctly since they are fields that I dynamically insert
How to capture the data from my dynamic table, traverse the array and save it correctly?