Do you get any error ? Could you please share your form code ?
How to save a belongsToMany relationship?
I have a belongsToMany relationship between part and tool with a pivot table.
A piece is manufactured with one or more tools. What I intend to achieve at this moment is to associate, relate all those tools that I dynamically enter in my form with their corresponding piece.
This is my dynamic table in my form
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Detalles de herramientas</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>Calidad</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><input type="text" name="quality[]" placeholder="Calidad" class="form-control" /></td>
<td><button type="button" name="add" id="add" class="btn btn-success"><i class="fa fa-plus-square"></i></button></td>
</tr>
</table>
</div>
</div>
In the store method I basically do the following:
I capture the data entered dynamically from my form that come in array format, these are insert and tool, a tool can have one or more inserts, then I capture the data entered, I associate the insert with its tool, then I intend to associate those tools entered to his corresponding piece, but I haven't achieved it yet
I've tried this with no effect:
$piece->tools()->sync($request->$tools);
This is my controller: store method
public function store(Request $request)
{
if ($request->ajax()){
try {
// Transacciones
DB::beginTransaction();
// dd($request->all());
// dynamic fields
$position = $request->position;
$code_tool = $request->code_tool;
$code_insert = $request->code_insert;
$quality = $request->quality;
// I walk the array, I create the insert then I associate it with its corresponding tool
for($count = 0; $count < count($position); $count++)
{
$insert = array(
'code_insert' => $code_insert[$count],
'quality' => $quality[$count]
);
$insert = new Insert();
$insert->code_insert = $code_insert[$count];
$insert->quality = $quality[$count];
if($insert->save()){
$tool = array(
'position' => $position[$count],
'code_tool' => $code_tool[$count],
'insert_id' => $insert->id
);
$tool_data[] = $tool;
}
}
// dd($tool_data);
$tools = Tool::insert($tool_data);
$gag = Gag::create($request->all());
$program = Program::create($request->all());
$piece = $program->piece()->create([
'denomination' => $request['denomination'],
'code' => $request['code'],
'time' => $request['time'],
'part_piece' => $request['part_piece'],
'gag_id' => $gag->id
]);
dd($piece);
// at this point you should associate all the tools entered to their corresponding part
$piece->tools()->sync($request->$tools);
DB::commit();
} catch (Exception $e) {
// anula la transacion
DB::rollBack();
}
}
}
just doing this before associating it
dd($piece);
$piece->tools()->sync($request->$tools);
I get the following:
Piece {#1062
#fillable: array:7 [
0 => "program_id"
1 => "gag_id"
2 => "denomination"
3 => "code"
4 => "part_piece"
5 => "time"
6 => "observation"
]
#connection: "mysql"
#table: "pieces"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:9 [
"denomination" => "prueba6"
"code" => "prueba6"
"time" => "01:46:18"
"part_piece" => "1"
"gag_id" => 9
"program_id" => 8
"updated_at" => "2020-01-29 21:53:14"
"created_at" => "2020-01-29 21:53:14"
"id" => 8
]
#original: array:9 [
"denomination" => "prueba6"
"code" => "prueba6"
"time" => "01:46:18"
"part_piece" => "1"
"gag_id" => 9
"program_id" => 8
"updated_at" => "2020-01-29 21:53:14"
"created_at" => "2020-01-29 21:53:14"
"id" => 8
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
How should I associate the tools used in the manufacture of the piece?
these are my models model Piece
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Piece extends Model
{
protected $fillable = [
'program_id', 'gag_id', 'denomination', 'code', 'part_piece', 'time', 'observation'
];
public function program()
{
return $this->belongsTo(Program::class);
}
public function gag()
{
return $this->belongsTo(Gag::class);
}
public function tools()
{
return $this->belongsToMany(Tool::class)->withTimestamps();
}
}
model Tool
<?php
namespace App;
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();
}
}
I need your help, thanks
Please or to participate in this conversation.