rjruiz
2 weeks ago
341
16
Laravel

how get value from dynamic fields?

Posted 2 weeks ago by rjruiz

Within my form I have a dynamic table that allows me to add as many dynamic fields as I want through jquery.

I need to iterate over each field that has an array format and get each value entered in order to edit the information

This dynamic table allows to register the tools that are used for the manufacture of a piece.

So the relationship between Piece and Tool is a many to many relationship. And in turn there is a one-to-many relationship with Insert and Tools

This is my dynamic table in my form:

<div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">Herramientas</h3>                
            </div>
            <div class="box-body">
                <table class="table table-striped table-bordered table-condensed table-hover" id="dynamicTable">  
                    <tr>
                        <th width="5%">PosiciĆ³n</th>
                        <th width="20%">Herramienta</th>                 
                        <th width="20%">Inserto</th>                 
                        <th width="20%">Calidad</th>                 
                        <th width="5%">Action</th>
                    </tr>
                    <tr>
                        <td><input type="text" name="addmore[0][position]"  placeholder="Posicion" class="form-control select2" /></td>                             
                        <td><input type="text" name="addmore[0][code_tool]"   placeholder="Herramienta" class="form-control" /></td>      
                        <td><input type="text" name="addmore[0][code_insert]"  placeholder="Inserto" class="form-control" /></td>  
                        <td><input type="text" name="addmore[0][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>

This is my edit method: With this I have all the information I need to recover ('piezas' => Piece::with(['tools.insert'])->get(),)

public function edit($id)
    {          
        $piece = Piece::findOrFail($id);  
        
        return view('admin.pieces.partials.form', [  
            'piezas'        => Piece::with(['tools.insert'])->get(),       
            'part_piece'    => Piece::pluck('part_piece', 'part_piece'), 
            'denomination'  => Piece::pluck('denomination', 'denomination'), 

            'type_gag'      => Gag::pluck('type_gag', 'type_gag'),           
            'category_gag'  => Gag::pluck('category_gag', 'category_gag'), 
           
            'part_program'  => Program::pluck('part_program', 'part_program'),                      
            'piece'         => $piece
        ]);  
    }

this doesn't work for me:

@foreach ($piezas as $pieza)
                        @foreach ($pieza->tools as $tool)         
                        <td><input type="text" name="addmore[0][position]" value="{{ $tool['position'] }}" placeholder="Posicion"  class="form-control select2" /></td>                             
                        <td><input type="text" name="addmore[0][code_tool]" value="{{ $tool['code_tool'] }}"  placeholder="Herramienta"class="form-control" /></td>      
                        <td><input type="text" name="addmore[0][code_insert]" value="{{ $tool->insert['code_insert'] }}" placeholder="Inserto" class="form-control" /></td>  
                        <td><input type="text" name="addmore[0][quality]" value="{{ $tool->insert['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>
                    
                        @endforeach                        
                     
                        @endforeach

How to navigate these fields in array format?

Please sign in or create an account to participate in this conversation.