【问题标题】:Where in laravel 5.2在 laravel 5.2 中
【发布时间】:2017-08-21 23:46:44
【问题描述】:

我正在尝试从出现错误的数据库中检索邮政编码 - 传递给 Illuminate\Database\Grammar::parameterize() 的参数 1 必须是数组类型,给定字符串。我不确定我的查询位置是否正确。提前致谢。

$countiesList 数组看起来像 = ["Beaverhead", "Big Horn",......]

模型中的功能是:

public static function counties_zip($countiesList)
    {

        $zipCodes= Zip::select('ZIPCode')
                  ->distinct()
                  ->whereIn('CountyName', $countiesList)
                  ->orderBy('ZIPCode', 'asc')
                  ->get();
        return $zipCodes;
    }

在控制器中:

public function zipCodes(Request $request) {


        $zipCodes = Zip::counties_zip($request->counties);


        return json_encode($zipCodes);

    }

还有我的 jS:

$(".submit").on("click", function(){
        myList = [];
        $('.select2 option:selected').each(function() {
            myList.push($(this).val())
        });
        $countiesList=myList;


        console.log($countiesList);
        $.ajax({
                    type: "get",
                    url: http_host + '/leads/regions/counties/zipcodes?counties=' + $countiesList,
                    data: { counties: $countiesList},
                    dataType: "json",
                    success: function (data) {
                        var htmlText = '';
                        for ( var i=0;i<data.length;i++ ) {
                            htmlText += '<div class="div-conatiner">';
                            htmlText += '<input type="checkbox">' +  data[i].ZipCode;
                            htmlText += '<div>';
                        }
                        $(".main-div").append(htmlText);
                    }
                });
    });

【问题讨论】:

    标签: javascript jquery arrays json laravel-5.2


    【解决方案1】:

    我认为这是因为您使用 GET 请求将多个 counties 值传递给您的控制器,但没有在请求中使用正确的 counties[] 参数。目前,如果您要在函数中执行dd($countiesList);,您将不会获得数组,因为您没有将数组传递给它;你传递了一个值。

    要将参数指定为GET 请求中的数组,您需要执行以下操作:

    /leads/regions/counties/zipcodes?counties[]=one&counties[]=two...
    

    对于您尝试过滤到的每个 counties 值。

    您可能希望将其构建为 POST 请求,如

    data : {
      counties: $countieList
    }
    

    应作为$.post(url, data, function(response){ ... });$.ajax(...) 请求中的值数组正确处理。

    【讨论】:

    • 感谢蒂姆的解决方案。我坚持获取并使用 JSON.stringify 将 javascript 数组转换为 JSON 数组。然后在模型中,我使用 json_decode 解码了 JSON 字符串。
    • 没问题;很高兴你解决了。顺便说一句,请随时接受您发布的答案;该问题将被标记为已关闭,这有助于未来有类似问题的访问者更快地找到解决方案。干杯。
    【解决方案2】:

    在 javascript 中,我使用 JSON.stringify 将 javascript 数组转换为 JSON 数组。然后在模型中,我使用 json_decode 解码了 JSON 字符串。 JS:

     $(".submit").on("click", function(){
                myList = [];
                $('.select2 option:selected').each(function() {
                    myList.push($(this).val())
                });
                $countiesList=myList;
    
    
                console.log($countiesList);
                $.ajax({
                            type: "get",
                            url: http_host + '/leads/regions/counties/zipcodes?counties=' + $countiesList,
                            data: { counties: $countiesList},
                            dataType: "json",
                            success: function (data) {
                                var htmlText = '';
                                for ( var i=0;i<data.length;i++ ) {
                                    htmlText += '<div class="div-conatiner">';
                                    htmlText += '<input type="checkbox">' +  data[i].ZipCode;
                                    htmlText += '<div>';
                                }
                                $(".main-div").append(htmlText);
                            }
                        });
            });
    

    型号:

    public static function counties_zip($countiesList)
        {
            $county= json_decode($countiesList, true); 
            $zipCodes= Zip::select('ZIPCode')
                      ->distinct()
                      ->whereIn('CountyName', $county)
                      ->orderBy('ZIPCode', 'asc')
                      ->get();
            return $zipCodes;
        }
    

    【讨论】:

      猜你喜欢
      • 2016-12-15
      • 2016-08-23
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2016-08-31
      • 2018-01-13
      相关资源
      最近更新 更多