我有一个公司比较网络应用程序,允许用户在不同的选择下拉列表中选择2家公司并将它们并排比较。到目前为止,一切都运作良好,除了订购。我希望比较的结果基于首先查询的项目,而不是默认的Laravel created_by字段。下面是我的查询生成器代码:
public function compareCompanies(Request $request) {
if ($request->has('companies') && $request->companies != null ) {
$companies = array_filter($request->companies);
$brands = Brand::with('categories')
->whereIn('brands.slug', $companies)
->get();
return response()->json(['success' => true, 'brands' => $brands]);
}
}
以下是我用于显示POST请求响应的ajax
$.ajax({
type: "POST",
url : url,
data: $("#compareForm").serialize(),
dataType : 'json',
success : function(response) {
if(response.success)
{
$("div#divLoading").removeClass('show');
$.each(response.brands, function(i, brand){
var NewRow = '';
NewRow += '<table class="table"><thead><tr>';
NewRow += '<th>' + brand.brand + '</th>';
NewRow += '</tr></thead><tbody>';
NewRow += '<tr><td>'+brand.growth_score+'</td></tr>';
NewRow += '<tr><td>'+brand.mind_share +'</td></tr>';
NewRow += '<tr><td>'+brand.headquarters+'</td></tr>';
NewRow += '<tr><td>'+brand.revenue+'</td></tr>';
NewRow += '<tr><td>'+brand.no_of_employees+'</td></tr>';
NewRow += '</tbody></table></div>';
$("#companies").append(NewRow);
})
}
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
}
});
如您所见,结果是交换的,因为Laravel使用created_at的值进行排序。如何根据输入顺序订购结果?
答案 0 :(得分:0)
Eloquent允许您通过在使用orderBy方法时指定列来订购查询结果,如下所示:
import QtQuick 2.8
...
import QtCharts 2.2
//I've removed unnecessary QML elements for simplicity
ChartView {
id: chartView
width: parent.width
height: parent.height
anchors.fill: parent
margins.bottom: 0
margins.top: 0
margins.left: 0
margins.right: 0
animationOptions: ChartView.NoAnimation
antialiasing: true
legend.visible: false
backgroundColor: "#1f1f1f"
ValueAxis {
id: axisY1
min: 0
max: 100
gridVisible: false
color: "#ffffff"
labelsColor: "#ffffff"
labelFormat: "%.0f"
}
ValueAxis {
id: axisX
min: 0
max: 50
gridVisible: false
color: "#ffffff"
labelsColor: "#ffffff"
labelFormat: "%.0f"
tickCount: 5
}
LineSeries {
id: lineSeries1
name: "signal 1"
color: "white"
axisX: axisX
axisY: axisY1
}
}
因此,如果您使用请求提交列名,则可以动态更改数据应按哪个列排序:
$brands = Brand::with('categories')
->whereIn('brands.slug', $companies)
->orderBy('column_name')
->get();