Evening All, after battling with Ajax for a few days i’ve submitted to flash notifications instead & only using ajax to populate a modal with the record data. However i have 2 issues that i’m struggling to resolve.
Issue #1: When i go to update a record, it only updates the last record in the table. When i hover over the edit button i can see that i’m opening record #24, i click, the modal opens, populates the data via Ajax and when i make any amendments it updates another record with the info i’ve amended.
Issue #2. When i try to update the record, i’ve tried to set my validation & logic to say if there is an existing file, don’t update the file but change the other fields however if there is a file, update all fields including the file name.
Please could one of you kind folk advise on how i can fix this…
Controller softwareAction.update
public function update(Request $request, $id)
{
$image_name = $request->hidden_image;
$image = $request->file('sw_link');
if($image != '')
{
$request->validate([
'sw_os' => 'required',
'sw_manual' => 'required',
'sw_type' => 'required',
'status' => 'required',
'sw_link' => 'file|max:9048'
]);
$image_name = 'Modified' . '.' . $image->getClientOriginalName();
$image->move(public_path('admin/software'), $image_name);
}
else
{
$request->validate([
'sw_link' => '',
'sw_os' => 'required',
'sw_manual' => 'required',
'status' => 'required',
'sw_type' => 'required'
]);
}
$form_data = array(
'sw_os' => $request->sw_os,
'sw_manual' => $request->sw_manual,
'sw_type' => $request->sw_type,
'status' => $request->status,
'sw_link' => $image_name
);
TuningSoftware::whereId($id)->update($form_data);
return redirect('dealersw')->with('info', 'Software Updated Successfully.');
}
Table & buttons in view:
<div id="displayed" class="tab-pane active">
<table class="table table-dark table-no-more table-bordered table-striped mb-0 text-center table-sm">
<thead>
<tr>
<th scope="col">Type</th>
<th scope="col">Link</th>
<th scope="col">Manual</th>
<th scope="col">OS</th>
<th scope="col">Status</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody id="users-crud">
@foreach($softwareD as $software)
<td data-title="Type">{{ $software->sw_type }}</td>
<td data-title="Link"><a download href="{{ URL::to('/') }}/admin/software/{{ $software->sw_link }}">Download</a></td>
<td data-title="Manual">{{ $software->sw_manual }}</td>
<td data-title="OS">{{ $software->sw_os }}</td>
<td data-title="Status">{{ $software->status }}</td>
<td>
<a href="javascript:void(0){{ $software->id }}" id="edit" data-toggle="modal" data-target="#modal-two" data-id="{{ $software->id }}" class="btn btn-sm btn-outline-info"><i class="far fa-edit"></i></a>
<a href="javascript:void(0){{ $software->id }}" id="delete" data-toggle="modal" data-target="#modal-three" data-id="{{ $software->id }}" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash-alt"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div><!-- end displayed pane -->
Edit Modal:
<!-- EDIT SOFTWARE MODAL -->
<div class="modal fade" id="edit-modal" tabindex="-1" role="dialog" aria-labelledby="edit-modal-label" aria-hidden="true" style="display: none;>
<div class="modal-dialog" role="document">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Update Software</h4>
</div>
<div class="modal-body">
<form method="post" action="{{ route('softwareAction.update', $software->id) }}" enctype="multipart/form-data">
@csrf
@method('PATCH')
<div class="form-group">
<input type="hidden" name="id" id="sw_id">
<div class="row">
<div class="col-sm-6">
<label>Operating System</label>
<select type="text" class="form-control" name="sw_os" id="sw_os">
<option hidden>Select</option>
<option>Mac</option>
<option>Windows</option>
</select>
</div><!-- end col -->
<div class="col-sm-6">
<label>Type</label>
<select type="text" class="form-control" name="sw_type" id="sw_type">
<option hidden>Select</option>
<option>Master</option>
<option>Slave</option>
</select>
</div><!-- end col -->
</div><!-- end row -->
<div class="padb-20"></div>
<div class="row">
<div class="col-sm-12">
<label>Manual</label>
<input type="text" class="form-control" name="sw_manual" id="sw_manual">
</div><!-- end col -->
</div><!-- end row -->
<input type="hidden" name="hidden-image" id="sw_link">
<div class="padb-20"></div>
<div class="row">
<div class="col-sm-12">
<label>Upload File <br></label><br>
<input type="file" name="sw_link">
</div>
</div><!-- end row -->
<div class="padb-20"></div>
<div class="row">
<div class="col-sm-12">
<label>Status</label>
<select type="text" class="form-control" name="status" id="status"">
<option hidden>Select</option>
<option value="Displayed">Displayed</option>
<option value="Not-Displayed">Not-Displayed</option>
</select>
</div><!-- end col -->
</div><!-- end row -->
</div><!-- end form-group -->
</div><!-- end modal body -->
<div class="modal-footer">
<button type="submit" class="btn btn-primary" value="Update">
Save changes
</button>
</div>
</form>
</div>
</div>
</div>
Ajax to populate the modal based on ID.
<script>
/* When click edit user */
$('body').on('click', '#edit', function () {
var softwareEnq_id = $(this).data('id');
$.get('softwareEnq/' + softwareEnq_id +'/edit', function (data) {
$('#modal-two').html("Edit Software Details");
// $('#btn-save').val("edit-user");
$('#edit-modal').modal('show');
$('#sw_id').val(data.id);
$('#sw_os').val(data.sw_os);
$('#sw_type').val(data.sw_type);
$('#sw_manual').val(data.sw_manual);
$('#sw_link').val(data.sw_link);
$('#status').val(data.status);
// console.log(data);
})
});
</script>
The thing is when i open my modal, i can see that its the correct data for that ID however i’m confused about why its passing another ID on the POST request. Also for reference, my route is a Resource::
Thank you in advance you lovely people.