从 Blade 控制器传递参数以筛选选择字段

huangapple go评论65阅读模式
英文:

Passing params from blade controller to filter a select field

问题

我有3个选择字段,sites(站点)、areas(区域)、devices(设备)
我需要根据sitesareas中的选择来过滤设备列表
因此,列表将仅包括来自该站点和该区域的设备。

已经使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_idsite_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(&#39;devices/workarea/{work_area_id?}/{site_id?}&#39;,&#39;DevicesController@getDevicesByArea&#39;)-&gt;name(&#39;devices.byArea&#39;);

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(&#39;id, CONCAT(device_alias) as work_device&#39;)
            -&gt;where(&#39;belongs_to_work_area_id&#39;,$work_area_code)
            -&gt;get()
            -&gt;pluck(&#39;work_device&#39;, &#39;id&#39;);
        }
    }
    return json_encode($devices);
}

JQuery:

$(document).ready(function () {
    $(&quot;#area_id&quot;).on(&#39;change&#39;, function(){
        var area_id = this.value;
        var site_id = $(&#39;#site_id&#39;).val();

        console.log(area_id)
        console.log(site_id)
        
        var deviceUrl = &quot;{{ route(&#39;admin.devices.byArea&#39;) }}/&quot; + area_id;

        $.ajax({
            url: deviceUrl,
            success: function(result){
                var html = &#39;&lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;&#39;;
                result = jQuery.parseJSON(result);
                console.log(result);
                for(var key in result) {
                    html += &#39;&lt;option value=&quot;&#39;+key+&#39;&quot;&gt;&#39;+result[key]+&#39;&lt;/option&gt;&#39;;
                }
                $(&quot;#device_id&quot;).html(html);
                $(&#39;#device_id&#39;).val(&#39;&#39;);
            }
        });
    });
});

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(&#39;id, CONCAT(device_alias) as work_device&#39;)
            -&gt;where(&#39;belongs_to_work_area_id&#39;,$work_area_code)
            -&gt;where(&#39;site_id&#39;,&#39;=&#39;,$site_id)    // &lt;&lt;&lt;&lt;&lt;&lt; change to your needs
            -&gt;get()
            -&gt;pluck(&#39;work_device&#39;, &#39;id&#39;);
        }
    }
    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 () {
    $(&quot;#area_id&quot;).on(&#39;change&#39;, function(){
        var area_id = this.value;
        var site_id = $(&#39;#site_id&#39;).val();

        console.log(area_id)
        console.log(site_id)
        // added site_id 
        var deviceUrl = &quot;{{ route(&#39;admin.devices.byArea&#39;) }}/&quot; + area_id +&quot;/&quot;+site_id;

        $.ajax({
            url: deviceUrl,
            success: function(result){
                var html = &#39;&lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;&#39;;
                result = jQuery.parseJSON(result);
                console.log(result);
                for(var key in result) {
                    html += &#39;&lt;option value=&quot;&#39;+key+&#39;&quot;&gt;&#39;+result[key]+&#39;&lt;/option&gt;&#39;;
                }
                $(&quot;#device_id&quot;).html(html);
                $(&#39;#device_id&#39;).val(&#39;&#39;);
            }
        });
    });
});

答案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(&#39;devices/workarea/{work_area_id?}/{site_id?}&#39;,&#39;DevicesController@getDevicesByArea&#39;)-&gt;name(&#39;devices.byArea&#39;);

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(&#39;id, CONCAT(device_alias) as work_device&#39;)
-&gt;where(&#39;belongs_to_work_area_id&#39;,$work_area_id)
//below code run only when site is not empty
//checking empty because possibly receive 0 or &#39;&#39;
//via jquery/javascript 
-&gt;when(!empty($site_id), function($query){
return $query-&gt;where(&#39;site_id&#39;, $site_id);
})
-&gt;get()
-&gt;pluck(&#39;work_device&#39;, &#39;id&#39;);
}
}
return json_encode($devices);
}

jQuery Code

$(document).ready(function () {
$(&quot;#area_id&quot;).on(&#39;change&#39;, function(){
var area_id = this.value;
var site_id = $(&#39;#site_id&#39;).val();
console.log(area_id)
console.log(site_id)
//added site_id 
var deviceUrl = &quot;{{ route(&#39;admin.devices.byArea&#39;) }}/&quot; + area_id + &quot;/&quot; + site_id;
$.ajax({
url: deviceUrl,
success: function(result){
var html = &#39;&lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;&#39;;
result = jQuery.parseJSON(result);
console.log(result);
for(var key in result) {
html += &#39;&lt;option value=&quot;&#39;+key+&#39;&quot;&gt;&#39;+result[key]+&#39;&lt;/option&gt;&#39;;
}
$(&quot;#device_id&quot;).html(html);
$(&#39;#device_id&#39;).val(&#39;&#39;);
}
});
});
});

huangapple
  • 本文由 发表于 2023年7月20日 19:58:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定