samedi 2 octobre 2021

laravel controller : update function for multiple dynamic rows

invoice_service Table - Invoice Table

the edit function works well in EditInvoice blade and the specific rows are displayed with their value. Example : if I choose to edit invoice N17 , in EditInvoice blade 2 results will be appear (see table image).

for the next step , I need to update this 2 results value(invoice services). I try many ways in update function but didnt work.

  • store function : works well
public function store(Request $request) : 
...
.....
$designation = $request->designation;
$QTE = $request->QTE;
$PU = $request->PU;
$total = $request->total;

$c = count($request->designation);
for ( $i=0; $i< $c; ++$i) { //get all rows(dynamic add/remove)
   $invoice_number = $request->invoice_number;
   $invoice_id = Invoices::latest()->first()->id;
   $services = new invoice_services();
   $services->invoice_number = $invoice_number;
   $services->invoice_id = $invoice_id;
   $services->designation = $designation[$i];
   $services->QTE = $QTE[$i];
   $services->PU = $PU[$i];
   $services->total = $QTE[$i] * $PU[$i];
   $services->save();
}   
..
...
  • Update function : need editing
public function update(Request $request )
    { 
//Here we need to update the rows !! I tried many ways but it didnt work
}
  • edit function : works well
public function edit($id) { 
$invoices = invoices::where('id', $id)->first();
$services = invoice_services::where('invoice_id' , $id)->get();
$clients = clients::all();
return view('invoices.edit_invoice', compact('clients', 'services' , 'invoices'));
}
  • edit_invoice view : works well
@foreach ($services as $service) 
       <tr>
   <td><input type="text" name="designation[]"  value="" class="form-control"></td>
  <td><input type="text" name="QTE[]" id="QTE'+incrementalRow+'" value="" class="form-control"></td>
   <td><input type="text" name="PU[]" id="PU'+incrementalRow+'" value="" class="form-control"></td>
   <td><input type="text" class="form-control form-control-lg" name="total[]" value="" id="totali'+incrementalRow+'" readonly ></td>
       </tr>
       @endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire