This should help you out on the HTML and jQuery part: https://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery
However for Laravel, make sure all your inputs are posted as an array. The tutorial also does that for you!
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Could you please suggest me a tutorial on the above mentioned
This should help you out on the HTML and jQuery part: https://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery
However for Laravel, make sure all your inputs are posted as an array. The tutorial also does that for you!
@bobbybouwmann is it possible to use only the
update function of the Controller
and the
edit view
to add dynamic forms and update them
Explain what you want to achieve?
This is something which i have done, but it's not working as i want yet
jobs.edit.blade.php
@foreach ($job->despatches as $despatch)
<div class="despatch-section col-sm-2">
<br>
<div class="form-group">
<label class="col-form-label" for="inputDefault">Despatch</label>
</div>
<div class="form-group">
<input type="text" class="form-control" name="despatch_no[]" id="inputDefault" placeholder="Despatch Number" value="{{ $despatch->id }}">
</div>
<div class="form-group">
<input type="text" class="form-control" name="despatch_date[]" id="inputDefault" placeholder="Despatch Date" value="{{ $despatch->despatch_date }}">
</div>
<div class="form-group">
<input type="text" class="form-control" name="despatch_qty[]" id="inputDefault" placeholder="Despatch Quantity" value="{{ $despatch->despatch_qty }}">
</div>
</div>
@endforeach
jquery part
<script>
$(document).ready(function() {
var i = 1;
$('#add-despatch').click(function(){
i++;
$('<div class="despatch-section col-sm-2">'+
'<br>'+
'<div class="form-group">'+
'<label class="col-form-label" for="inputDefault">Despatch</label>'+
'</div>'+
' <div class="form-group">'+
'<input type="text" class="form-control" name="despatch_no[]" id="inputDefault" placeholder="Despatch Number">'+
'</div>'+
'<div class="form-group">'+
'<input type="text" class="form-control" name="despatch_date[]" id="inputDefault" placeholder="Despatch Date">'+
'</div>'+
'<div class="form-group">'+
'<input type="text" class="form-control" name="despatch_qty[]" id="inputDefault" placeholder="Despatch Quantity">'+
'</div>'+
'</div>').appendTo('#container');
});
});
</script>
JobController.php
public function update(Request $request, $id)
{
$job = Job::find($id);
//$job->jobNumber = $request->jobNumber;
$job->customer_name = $request->customer_name;
$job->company_name = $request->company_name;
$job->job_type = $request->job_type;
$job->job_status = $request->job_status;
$job->job_delivery_date = $request->job_delivery_date;
$job->job_delivery_time = $request->job_delivery_time;
$job->pk_pkl = $request->pk_pkl;
//Assumes that the maximum number of despatches will be less than 5
for ($i=0; $i <5; $i++){
//check whether an existing dispatch no is there in the database
if(jDespatch::where('id', $request->despatch_no[$i])->get()->count() ){
//Update Record - If there is an existing record
DB::table('j_despatches')
->where('job_id',$id)
->where('id',$request->despatch_no[$i])
->update(
[
'despatch_date' => $request->despatch_date[$i],
'despatch_qty' => $request->despatch_qty[$i]
]);
//dd($request);
$job->save();
return redirect()->route('job.edit', $job->id);
}
else {
//Insert Record
DB::table('j_despatches')
->insert(
[
'id' => $request->despatch_no[$i],
'despatch_date' => $request->despatch_date[$i],
'despatch_qty' => $request->despatch_qty[$i],
'job_id' => $id
]);
$job->save();
return redirect('job');
}
}
$job->save();
return redirect('job');
}
Well this is the way to go. You need to post everything as an array and insert it into the database. What is currently not working for you?
Update and Insert is mentioned inside JobController - update function
I am not sure it is the right way to do this
Please or to participate in this conversation.