我有这个雄辩的电话:
<script>
jQuery("#select1").on('change',
function(event)
{
event.preventDefault();
jQuery.ajax({
type:'POST',
async: true,
cache: false,
url: '<?php echo Router::Url(array('controller' => 'XXX','admin' => TRUE, 'action' => 'onChangeSelect1Ajax'), TRUE); ?>',
success: function(response) {
jQuery('#select2Position').empty().append(response);
},
error: function(response) {
alert('error');
},
data:{ idselect1 : this.value }
});
return false;
}
);
jQuery("#select2").on('change',
function(event)
{
event.preventDefault();
jQuery.ajax({
type:'POST',
async: true,
cache: false,
url: '<?php echo Router::Url(array('controller' => 'XXX','admin' => TRUE, 'action' => 'onChangeSelect12Ajax'), TRUE); ?>',
success: function(response) {
jQuery('#other').empty().append(response);
},
error: function(response) {
alert('error');
},
data:{ idselect1 : jQuery("#select1").value, idselect2 : this.value }
});
return false;
}
);
</script>
但是现在我希望结果按照排序数组的顺序排序;所以结果应该是:
$array = [100, 200, 50, 3, 300];
$response = EloquentModel::whereIn('id', $array)->get();
订单是由$ array变量如何排序的元素指定的......
是否有可能在Eloquent中做类似的事情?
答案 0 :(得分:0)
好的,所以解决方案如下:
$response = EloquentModel::whereIn('id', $array)->get()->sortBy(function($item, $index) use($array){
$arrayToSortBy = array_flip($array);
return $arrayToSortBy[$item->id];
});
基本上我们可以对Eloquent:Collections回复进行排序。
array_flip
将使用值翻转键,以便我们可以返回相关的数字进行排序....