英文:
Passing params from blade controller to filter a select field
问题
我有3个选择字段,sites
(站点)、areas
(区域)、devices
(设备)
我需要根据sites
和areas
中的选择来过滤设备列表
因此,列表将仅包括来自该站点和该区域的设备。
已经使areas
(区域)正常工作,但尝试在JS
中传递另一个sites
(站点)变量时出现500错误
路由:
Route::get('devices/workarea/{work_area_id?}/{site_id?}','DevicesController@getDevicesByArea')->name('devices.byArea');
控制器:
public function getDevicesByArea($work_area_code = NULL, $site_id = NULL) {
$devices = array();
if($work_area_code != NULL && $site_id != NULL){
$workArea = WorkArea::find($work_area_code);
$site = Site::find($site_id);
if($workArea && $site) {
$devices = Device::selectRaw('id, CONCAT(device_alias) as work_device')
->where('belongs_to_work_area_id', $work_area_code)
->where('belongs_to_site_id', $site_id)
->get()
->pluck('work_device', 'id');
}
}
return json_encode($devices);
}
jQuery:
$(document).ready(function () {
$("#area_id").on('change', function(){
var area_id = this.value;
var site_id = $('#site_id').val();
console.log(area_id)
console.log(site_id)
var deviceUrl = "{{ route('admin.devices.byArea') }}/" + area_id + "/" + site_id;
$.ajax({
url: deviceUrl,
success: function(result){
var html = '<option value="">Please select</option>';
result = jQuery.parseJSON(result);
console.log(result);
for(var key in result) {
html += '<option value="'+key+'">'+result[key]+'</option>';
}
$("#device_id").html(html);
$('#device_id').val('');
}
});
});
});
如何正确发送第二个所需的参数site_id
到控制器,并同时通过area_id
和site_id
来筛选设备?
英文:
I have 3 selection fields, sites
, areas
, devices
I need to filter the devices list according to what was selected in sites
and areas
so the list will include only devices from that site and that area only.
Made it work only with areas
but getting error 500 when trying to pass another variable for sites
in JS
Route:
Route::get('devices/workarea/{work_area_id?}/{site_id?}','DevicesController@getDevicesByArea')->name('devices.byArea');
Controller:
public function getDevicesByArea($work_area_code = NULL) {
$devices = array();
if($work_area_code != NULL){
$workArea = WorkArea::find($work_area_code);
if($workArea) {
$devices = Device::selectRaw('id, CONCAT(device_alias) as work_device')
->where('belongs_to_work_area_id',$work_area_code)
->get()
->pluck('work_device', 'id');
}
}
return json_encode($devices);
}
JQuery:
$(document).ready(function () {
$("#area_id").on('change', function(){
var area_id = this.value;
var site_id = $('#site_id').val();
console.log(area_id)
console.log(site_id)
var deviceUrl = "{{ route('admin.devices.byArea') }}/" + area_id;
$.ajax({
url: deviceUrl,
success: function(result){
var html = '<option value="">Please select</option>';
result = jQuery.parseJSON(result);
console.log(result);
for(var key in result) {
html += '<option value="'+key+'">'+result[key]+'</option>';
}
$("#device_id").html(html);
$('#device_id').val('');
}
});
});
});
How to correctly send the second needed parameter site_id
to controller and filter the devices at the same time by both area_id
and site_id
?
答案1
得分: 2
你需要将参数添加到方法中。根据需要更新你的查询,并确保从URL中获取的两个值都进行输入值的净化。
public function getDevicesByArea($work_area_code = NULL, $site_id = NULL) {
$devices = array();
if($site_id == NULL){
// 当未提供site_id时,设置默认值为1
$site_id = 1;
}
if($work_area_code != NULL){
$workArea = WorkArea::find($work_area_code);
if($workArea) {
$devices = Device::selectRaw('id, CONCAT(device_alias) as work_device')
->where('belongs_to_work_area_id',$work_area_code)
->where('site_id','=',$site_id) // <<<<<<< 根据你的需求更改
->get()
->pluck('work_device', 'id');
}
}
return json_encode($devices);
}
jQuery
你需要将参数添加到URL中,以便发送到方法。
$(document).ready(function () {
$("#area_id").on('change', function(){
var area_id = this.value;
var site_id = $('#site_id').val();
console.log(area_id)
console.log(site_id)
// 添加了 site_id 参数
var deviceUrl = "{{ route('admin.devices.byArea') }}/" + area_id + "/" + site_id;
$.ajax({
url: deviceUrl,
success: function(result){
var html = '<option value="">请选择</option>';
result = jQuery.parseJSON(result);
console.log(result);
for(var key in result) {
html += '<option value="'+key+'">'+result[key]+'</option>';
}
$("#device_id").html(html);
$('#device_id').val('');
}
});
});
});
英文:
You need to add the parameter to the method. Update your query as needed, and also sanitize the input value from both values you are getting in the url.
public function getDevicesByArea($work_area_code = NULL, $site_id = NULL) {
$devices = array();
if($site_id == NULL){
// default site_id when none is present
$site_id = 1;
}
if($work_area_code != NULL){
$workArea = WorkArea::find($work_area_code);
if($workArea) {
$devices = Device::selectRaw('id, CONCAT(device_alias) as work_device')
->where('belongs_to_work_area_id',$work_area_code)
->where('site_id','=',$site_id) // <<<<<< change to your needs
->get()
->pluck('work_device', 'id');
}
}
return json_encode($devices);
}
jQuery
You need to add the parameter to the url, to be able to be send to the method.
$(document).ready(function () {
$("#area_id").on('change', function(){
var area_id = this.value;
var site_id = $('#site_id').val();
console.log(area_id)
console.log(site_id)
// added site_id
var deviceUrl = "{{ route('admin.devices.byArea') }}/" + area_id +"/"+site_id;
$.ajax({
url: deviceUrl,
success: function(result){
var html = '<option value="">Please select</option>';
result = jQuery.parseJSON(result);
console.log(result);
for(var key in result) {
html += '<option value="'+key+'">'+result[key]+'</option>';
}
$("#device_id").html(html);
$('#device_id').val('');
}
});
});
});
答案2
得分: 1
Route
Route::get('devices/workarea/{work_area_id?}/{site_id?}','DevicesController@getDevicesByArea')->name('devices.byArea');
Code in Controller
public function getDevicesByArea($work_area_id = null, $site_id = null) {
$devices = [];
if(!empty($work_area_id)){
$workArea = WorkArea::find($work_area_id);
if($workArea) {
$devices = Device::selectRaw('id, CONCAT(device_alias) as work_device')
->where('belongs_to_work_area_id',$work_area_id)
//below code run only when site is not empty
//checking empty because possibly receive 0 or ''
//via jquery/javascript
->when(!empty($site_id), function($query) use ($site_id){
return $query->where('site_id', $site_id);
})
->get()
->pluck('work_device', 'id');
}
}
return json_encode($devices);
}
jQuery Code
$(document).ready(function () {
$("#area_id").on('change', function(){
var area_id = this.value;
var site_id = $('#site_id').val();
console.log(area_id)
console.log(site_id)
//added site_id
var deviceUrl = "{{ route('devices.byArea') }}/" + area_id + "/" + site_id;
$.ajax({
url: deviceUrl,
success: function(result){
var html = '<option value="">Please select</option>';
result = jQuery.parseJSON(result);
console.log(result);
for(var key in result) {
html += '<option value="'+key+'">'+result[key]+'</option>';
}
$("#device_id").html(html);
$('#device_id').val('');
}
});
});
});
英文:
Route
Route::get('devices/workarea/{work_area_id?}/{site_id?}','DevicesController@getDevicesByArea')->name('devices.byArea');
Code in Controller
public function getDevicesByArea($work_area_id = null, $site_id = null) {
$devices = [];
if(!empty($work_area_id)){
$workArea = WorkArea::find($work_area_id);
if($workArea) {
$devices = Device::selectRaw('id, CONCAT(device_alias) as work_device')
->where('belongs_to_work_area_id',$work_area_id)
//below code run only when site is not empty
//checking empty because possibly receive 0 or ''
//via jquery/javascript
->when(!empty($site_id), function($query){
return $query->where('site_id', $site_id);
})
->get()
->pluck('work_device', 'id');
}
}
return json_encode($devices);
}
jQuery Code
$(document).ready(function () {
$("#area_id").on('change', function(){
var area_id = this.value;
var site_id = $('#site_id').val();
console.log(area_id)
console.log(site_id)
//added site_id
var deviceUrl = "{{ route('admin.devices.byArea') }}/" + area_id + "/" + site_id;
$.ajax({
url: deviceUrl,
success: function(result){
var html = '<option value="">Please select</option>';
result = jQuery.parseJSON(result);
console.log(result);
for(var key in result) {
html += '<option value="'+key+'">'+result[key]+'</option>';
}
$("#device_id").html(html);
$('#device_id').val('');
}
});
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论