Hi, I'm after some advice about the best way to handle some Ajax requests in a project I'm working on.
In the project I need to allow the user to 'toggle' various options which are dynamically changed via ajax.
For example, I may have the following options:
Show company name [On | Off]
Show address [On | Off]
Show phone number [On | Off]
Show email address [On | Off]
How would you suggest I set this type of thing up? I was thinking of creating one column (called 'employee_options') and then populate it with an array of objects such as…
[{showCompany: true , showAddress: true, showPhone: false, showEmail: true}]
…this would mean if I need to add more options in the future I won't need to add columns etc. Would this be best practice? Also, would I run into any validation problems if I sent the array like that?
Next, would I be correct in assuming the following would be the best way to set up my table:
-
Create new migration 'addOptionsToEmployees' and add 'employee_options' as the column.
-
When creating a new employee I would automatically pass default options ( so this would be [{showCompany: true , showAddress: true, showPhone: false, showEmail: true}] etc) in the store part of the controller.
-
When editing an existing employee I wouldn't want to overwrite the options already there, so should I simply leave the 'employee_options' column out of the update request or would that throw an error as nothing is being saved into the column?
Finally, this is the normal setup I have for sending data to the server:
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
});
$.ajax({
type: '',
url: '',
success: function(returnedData){
},
error: function(error){
}
});
When the user isn't adding or editing an employee and they're just changing the options using the 'show' view via toggle switches, what is the best way to send the data to the server? Should I do it as per the following…
ajax url: '/employees/options/'+employeeId+'/'+showCompany+'/'+showAddress+'/'+showPhone+'/'+showEmail,
Route::post('/employees/options/{employeeId}/{showCompany}/{showAddress}/{showPhone}/{showEmail}',['middleware'=>'check-permission:user|admin|superadmin','uses'=>'EmployeeController@employeeOptions']);
…with a controller function like:
public function employeeOptions(Request $request, $employeeId, $showCompany, $showAddress, $showPhone, $showEmail){
// Do stuff
}
…or is there a more efficient way of doing it?
I'll also need to format the data in the //Do stuff part (above) before I insert it into the employee_options column; how would you suggest I format this insert statement?
Sorry for asking so many questions, I'm gradually getting to grips with Laravel but want to make sure I'm not developing poor coding skills. Hopefully other members will also find this thread of use. :)