使用laravel db query

时间:2015-07-09 10:31:22

标签: php jquery html json laravel-5

我有一个html选择框,需要使用jquery ajax和来自php(Laravel)数据库查询的select选项进行填充。我尝试了很多方法但没有结果,我只是得到了“未定义的”#39;结果。 Laravel脚本:

public function loturi($articol) {
$loturi = DB::select("select mgla_lotto from MG_V_DispoLottiBarcode d
join MG_AnaArt art on art.MGAA_Id = d.MGAA_Id join MG_LottiAnag  l on         
l.MGLA_Id = d.MGLA_Id where MGAA_MBDC_Classe IN ('SEM','FIN') and mbmg_id = 55 
and mgaa_matricola in (select  child.MGAA_Matricola component
from DB_Legami join MG_AnaArt  child on child.MGAA_Id = DBLG_Com_MGAA_Id
join MG_AnaArt  p on p.MGAA_Id = DBLG_MGAA_Id
where p.MGAA_Matricola = '$articol')  and mgla_datacreazione > '2014-01-01'
group by mgla_lotto having sum(dispo) > 0");
return compact('loturi');
}

结果如下:

{"loturi":[{"mgla_lotto":"1282\/15"},{"mgla_lotto":"1316\/15"},{"mgla_lotto":"1339\/15"},{"mgla_lotto":"1349\/15"},{"mgla_lotto":"1354\/15"},{"mgla_lotto":"1404\/15"},{"mgla_lotto":"1405\/15"},{"mgla_lotto":"1412\/15"}]}

jquery脚本是这样的:

$(document).ready(function() {
$("#cod").change(function(){
var cod_articol = $("#cod").val();
$.ajax ({
url: "/internal/" + cod_articol ,
datatype: "json",
success: function(data) {
$.each(data, function(value){
$("#lot").append('<option id="' + value.index + '">' + value.name +   
'</option>');
      })
     }
   });
 });
});

1 个答案:

答案 0 :(得分:3)

嗨,这是你的答案。

变化

$.each(data, function(value){

$.each(data.loturi, function(value){

并使用以下语法

$.each(data.loturi, function( index, value ){
    $("#lot").append('<option value="' + index + '">' + value.mgla_lotto  + '</option>');
}

点击链接查看我为您制作的示例。

https://jsfiddle.net/ovht7fkh/2/