Javascript laravel Pull()返回一行而不是集合
我正在开发一个下拉列表,为此我需要一个键值中的json格式,这就是为什么我使用了pulk()helper函数,但它不能准确地工作,它只返回一行 当我尝试查询以下查询时,它显示它不应该显示limit(),因为我没有使用它 控制器Javascript laravel Pull()返回一行而不是集合,javascript,jquery,json,ajax,laravel,Javascript,Jquery,Json,Ajax,Laravel,我正在开发一个下拉列表,为此我需要一个键值中的json格式,这就是为什么我使用了pulk()helper函数,但它不能准确地工作,它只返回一行 当我尝试查询以下查询时,它显示它不应该显示limit(),因为我没有使用它 控制器 { DB::enableQueryLog(); $sections = DB::table("sections")->where("division_id",$request->division)->pluck("name","id");
{
DB::enableQueryLog();
$sections = DB::table("sections")->where("division_id",$request->division)->pluck("name","id");
dd(DB::getQueryLog());
return response()->json($sections);
}
因为我使用了Pull,下面的查询实际上正在运行
array:1 [▼
0 => array:3 [▼
"query" => "select `name` from `sections` where `division_id` = ? limit 1"
"bindings" => array:1 [▼
0 => "1"
]
"time" => 0.0
]
]
然而,我需要JSON键值对中的结果,以便在下面的java脚本中使用它
JS
$('#除法')。更改(函数(){
var divisionID=$(this.val();
if(部门ID){
$.ajax({
键入:“获取”,
数据类型:“json”,
url:“{url('get-section-list')}}division=“+divisionID,
成功:功能(res){
如果(res){
$(“#节”).empty();
$(“#节”).append('Select');
$.each(res、函数(键、值){
$(“#节”)。追加(“”+value+“”);
});
}否则{
$(“#节”).empty();
}
}
});
}否则{
$(“#节”).empty();
}
});
我试过这个,因为Pull()不是;t工作,我需要键值中的JSON,所以首先我在集合中获得转换后的数组,然后应用Pull()
}控制器中的
//should just make a model for sections then you could just do:
// return Section::select('name', 'id')->where("division_id",$request->division)->get();
//controller will output it as json by default
return DB::table("sections")->select('name', 'id')->where("division_id",$request->division)->get();
视图:
//您不需要键值对象,只需在结果上循环并按属性引用它,如下所示
<script type="text/javascript">
$('#division').change(function(){
var divisionID = $(this).val();
if(divisionID){
$.ajax({
type:"GET",
dataType: "json",
url:"{{url('get-section-list')}}?division="+divisionID,
success:function(res){
if(res){
$("#section").empty();
$("#section").append('<option>Select</option>');
$.each(res,function(placement,row){
$("#section").append('<option value="'+row.id+'">'+row.name+'</option>');
});
}else{
$("#section").empty();
}
}
});
}else{
$("#section").empty();
}
});
</script>
$('#除法')。更改(函数(){
var divisionID=$(this.val();
if(部门ID){
$.ajax({
键入:“获取”,
数据类型:“json”,
url:“{url('get-section-list')}}division=“+divisionID,
成功:功能(res){
如果(res){
$(“#节”).empty();
$(“#节”).append('Select');
$.each(res,function(placement,row){
$(“#节”).append(“”+row.name+“”);
});
}否则{
$(“#节”).empty();
}
}
});
}否则{
$(“#节”).empty();
}
});
您使用的是值
而不是弹拨
,它们是两个不同的东西。弹拨也显示相同的结果。我以前试过,但错误地上传了值代码->pull('name','id')->get()
我可能会弄错,但我记得在我pull()之后运行get(),pull()之后运行get(),无法使用pull()不能在数组上使用,但在集合$query->pull('name',id')上使用pull
如果使用get()->pulk()
您使用的是集合pulk()
而不是查询生成器pulk()
,则应返回带有id
键和name
值的集合。什么版本的laravel?如果这不是一个答案,我建议您删除它并用新信息编辑您的原始问题,这样可以更容易地帮助您。$query->pull()
工作正常,您不需要检索集合并对其执行pull()。如果你的结果是真实的,你应该向laravel回购公司提出问题。
//should just make a model for sections then you could just do:
// return Section::select('name', 'id')->where("division_id",$request->division)->get();
//controller will output it as json by default
return DB::table("sections")->select('name', 'id')->where("division_id",$request->division)->get();
<script type="text/javascript">
$('#division').change(function(){
var divisionID = $(this).val();
if(divisionID){
$.ajax({
type:"GET",
dataType: "json",
url:"{{url('get-section-list')}}?division="+divisionID,
success:function(res){
if(res){
$("#section").empty();
$("#section").append('<option>Select</option>');
$.each(res,function(placement,row){
$("#section").append('<option value="'+row.id+'">'+row.name+'</option>');
});
}else{
$("#section").empty();
}
}
});
}else{
$("#section").empty();
}
});
</script>